Psr7Middlewares\Utils\Helpers::hashEquals PHP Метод

hashEquals() публичный статический Метод

Very short timing attack safe string comparison for PHP < 5.6 http://php.net/manual/en/function.hash-equals.php#118384.
public static hashEquals ( string $a, string $b ) : boolean
$a string
$b string
Результат boolean
    public static function hashEquals($a, $b)
    {
        if (self::$hash_equals === null) {
            self::$hash_equals = function_exists('hash_equals');
        }
        if (self::$hash_equals) {
            return hash_equals($a, $b);
        }
        return substr_count($a ^ $b, "") * 2 === strlen($a . $b);
    }

Usage Example

Пример #1
0
 /**
  * Decrypt the given value.
  *
  * @param string $value
  * 
  * @return string
  */
 private function decrypt($value)
 {
     $this->checkKey();
     $decoded = base64_decode($value);
     $hmac = mb_substr($decoded, 0, 32, '8bit');
     $iv = mb_substr($decoded, 32, 16, '8bit');
     $cipher = mb_substr($decoded, 48, null, '8bit');
     $calculated = hash_hmac('sha256', $iv . $cipher, $this->authentication, true);
     if (Helpers::hashEquals($hmac, $calculated)) {
         $value = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $cipher, 'ctr', $iv), "");
         return json_decode($value, true);
     }
 }
All Usage Examples Of Psr7Middlewares\Utils\Helpers::hashEquals