|
|
|
@ -80,10 +80,10 @@ class BaseSecurity
|
|
|
|
|
} |
|
|
|
|
$module = static::openCryptModule(); |
|
|
|
|
$ivSize = mcrypt_enc_get_iv_size($module); |
|
|
|
|
$iv = StringHelper::substr($data, 0, $ivSize); |
|
|
|
|
$iv = StringHelper::byteSubstr($data, 0, $ivSize); |
|
|
|
|
$key = static::deriveKey($password, $iv); |
|
|
|
|
mcrypt_generic_init($module, $key, $iv); |
|
|
|
|
$decrypted = mdecrypt_generic($module, StringHelper::substr($data, $ivSize, StringHelper::strlen($data))); |
|
|
|
|
$decrypted = mdecrypt_generic($module, StringHelper::byteSubstr($data, $ivSize, StringHelper::byteLength($data))); |
|
|
|
|
mcrypt_generic_deinit($module); |
|
|
|
|
mcrypt_module_close($module); |
|
|
|
|
return static::stripPadding($decrypted); |
|
|
|
@ -96,7 +96,7 @@ class BaseSecurity
|
|
|
|
|
*/ |
|
|
|
|
protected static function addPadding($data) |
|
|
|
|
{ |
|
|
|
|
$pad = self::CRYPT_BLOCK_SIZE - (StringHelper::strlen($data) % self::CRYPT_BLOCK_SIZE); |
|
|
|
|
$pad = self::CRYPT_BLOCK_SIZE - (StringHelper::byteLength($data) % self::CRYPT_BLOCK_SIZE); |
|
|
|
|
return $data . str_repeat(chr($pad), $pad); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -107,11 +107,11 @@ class BaseSecurity
|
|
|
|
|
*/ |
|
|
|
|
protected static function stripPadding($data) |
|
|
|
|
{ |
|
|
|
|
$end = StringHelper::substr($data, -1, NULL); |
|
|
|
|
$end = StringHelper::byteSubstr($data, -1, NULL); |
|
|
|
|
$last = ord($end); |
|
|
|
|
$n = StringHelper::strlen($data) - $last; |
|
|
|
|
if (StringHelper::substr($data, $n, NULL) == str_repeat($end, $last)) { |
|
|
|
|
return StringHelper::substr($data, 0, $n); |
|
|
|
|
$n = StringHelper::byteLength($data) - $last; |
|
|
|
|
if (StringHelper::byteSubstr($data, $n, NULL) == str_repeat($end, $last)) { |
|
|
|
|
return StringHelper::byteSubstr($data, 0, $n); |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
@ -164,11 +164,11 @@ class BaseSecurity
|
|
|
|
|
*/ |
|
|
|
|
public static function validateData($data, $key, $algorithm = 'sha256') |
|
|
|
|
{ |
|
|
|
|
$hashSize = StringHelper::strlen(hash_hmac($algorithm, 'test', $key)); |
|
|
|
|
$n = StringHelper::strlen($data); |
|
|
|
|
$hashSize = StringHelper::byteLength(hash_hmac($algorithm, 'test', $key)); |
|
|
|
|
$n = StringHelper::byteLength($data); |
|
|
|
|
if ($n >= $hashSize) { |
|
|
|
|
$hash = StringHelper::substr($data, 0, $hashSize); |
|
|
|
|
$data2 = StringHelper::substr($data, $hashSize, $n - $hashSize); |
|
|
|
|
$hash = StringHelper::byteSubstr($data, 0, $hashSize); |
|
|
|
|
$data2 = StringHelper::byteSubstr($data, $hashSize, $n - $hashSize); |
|
|
|
|
return $hash === hash_hmac($algorithm, $data2, $key) ? $data2 : false; |
|
|
|
|
} else { |
|
|
|
|
return false; |
|
|
|
|