/** * Compare two strings in constant time. * * @param string $string1 The first string to compare. * @param string $string2 The second string to compare. * @return bool */ public static function stringCompare($string1, $string2) { if (\Sodium\memcmp($string1, $string2) !== 0) { return false; } return true; }
/** * Compares two strings in a secure way. * * This is the same as PHP's strcmp() implementation, but it is resistant to * timing attacks. * * @link https://paragonie.com/book/pecl-libsodium/read/03-utilities-helpers.md#compare * @param string $str1 The first string * @param string $str2 The second string * @return bool */ public function stringCompare($str1, $str2) { // check variable type manually if (!is_string($str1) || !is_string($str2)) { return false; } /** @noinspection PhpUndefinedNamespaceInspection @noinspection PhpUndefinedFunctionInspection */ return \Sodium\memcmp($str1, $str2) === 0; }