сходу красоту навел

This commit is contained in:
rsgrinko committed 2022-07-14 00:19:41 +03:00
1 parent 9d7349867e
commit 5fbc3d0b14
1 file changed
+14 -14
+14 -14
View File
@@ -66,12 +66,12 @@
*/ */
public function encode(string $string): string public function encode(string $string): string
{ {
$ivlen = openssl_cipher_iv_length($this->method); $ivLength = openssl_cipher_iv_length($this->method);
$iv = openssl_random_pseudo_bytes($ivlen); $iv = openssl_random_pseudo_bytes($ivLength);
$ciphertext_raw = openssl_encrypt($string, $this->method, $this->key, OPENSSL_RAW_DATA, $iv); $cipherTextRaw = openssl_encrypt($string, $this->method, $this->key, OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac($this->algorithm, $ciphertext_raw, $this->key, true); $hmac = hash_hmac($this->algorithm, $cipherTextRaw, $this->key, true);
$ciphertext = base64_encode($iv . $hmac . $ciphertext_raw); $cipherText = base64_encode($iv . $hmac . $cipherTextRaw);
return $ciphertext; return $cipherText;
} }
/** /**
@@ -84,14 +84,14 @@
public function decode(string $string): ?string public function decode(string $string): ?string
{ {
$c = base64_decode($string); $c = base64_decode($string);
$ivlen = openssl_cipher_iv_length($this->method); $ivLength = openssl_cipher_iv_length($this->method);
$iv = substr($c, 0, $ivlen); $iv = substr($c, 0, $ivLength);
$hmac = substr($c, $ivlen, $sha2len = 32); $hmac = substr($c, $ivLength, $sha2len = 32);
$ciphertext_raw = substr($c, $ivlen + $sha2len); $cipherTextRaw = substr($c, $ivLength + $sha2len);
$plaintext = openssl_decrypt($ciphertext_raw, $this->method, $this->key, OPENSSL_RAW_DATA, $iv); $result = openssl_decrypt($cipherTextRaw, $this->method, $this->key, OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac($this->algorithm, $ciphertext_raw, $this->key, true); $calcMac = hash_hmac($this->algorithm, $cipherTextRaw, $this->key, true);
if (hash_equals($hmac, $calcmac)) { if (hash_equals($hmac, $calcMac)) {
return $plaintext; return $result;
} }
return null; return null;
} }