From 5fbc3d0b146e5a8bb00b52f007ca2c897378058b Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 14 Jul 2022 00:19:41 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D1=85=D0=BE=D0=B4=D1=83=20=D0=BA=D1=80?= =?UTF-8?q?=D0=B0=D1=81=D0=BE=D1=82=D1=83=20=D0=BD=D0=B0=D0=B2=D0=B5=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/lib/Core/Helpers/Crypto.class.php | 30 +++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/core/lib/Core/Helpers/Crypto.class.php b/core/lib/Core/Helpers/Crypto.class.php index af05389..9917668 100644 --- a/core/lib/Core/Helpers/Crypto.class.php +++ b/core/lib/Core/Helpers/Crypto.class.php @@ -66,12 +66,12 @@ */ public function encode(string $string): string { - $ivlen = openssl_cipher_iv_length($this->method); - $iv = openssl_random_pseudo_bytes($ivlen); - $ciphertext_raw = openssl_encrypt($string, $this->method, $this->key, OPENSSL_RAW_DATA, $iv); - $hmac = hash_hmac($this->algorithm, $ciphertext_raw, $this->key, true); - $ciphertext = base64_encode($iv . $hmac . $ciphertext_raw); - return $ciphertext; + $ivLength = openssl_cipher_iv_length($this->method); + $iv = openssl_random_pseudo_bytes($ivLength); + $cipherTextRaw = openssl_encrypt($string, $this->method, $this->key, OPENSSL_RAW_DATA, $iv); + $hmac = hash_hmac($this->algorithm, $cipherTextRaw, $this->key, true); + $cipherText = base64_encode($iv . $hmac . $cipherTextRaw); + return $cipherText; } /** @@ -83,15 +83,15 @@ */ public function decode(string $string): ?string { - $c = base64_decode($string); - $ivlen = openssl_cipher_iv_length($this->method); - $iv = substr($c, 0, $ivlen); - $hmac = substr($c, $ivlen, $sha2len = 32); - $ciphertext_raw = substr($c, $ivlen + $sha2len); - $plaintext = openssl_decrypt($ciphertext_raw, $this->method, $this->key, OPENSSL_RAW_DATA, $iv); - $calcmac = hash_hmac($this->algorithm, $ciphertext_raw, $this->key, true); - if (hash_equals($hmac, $calcmac)) { - return $plaintext; + $c = base64_decode($string); + $ivLength = openssl_cipher_iv_length($this->method); + $iv = substr($c, 0, $ivLength); + $hmac = substr($c, $ivLength, $sha2len = 32); + $cipherTextRaw = substr($c, $ivLength + $sha2len); + $result = openssl_decrypt($cipherTextRaw, $this->method, $this->key, OPENSSL_RAW_DATA, $iv); + $calcMac = hash_hmac($this->algorithm, $cipherTextRaw, $this->key, true); + if (hash_equals($hmac, $calcMac)) { + return $result; } return null; }