Replaced mcrypt with openssl (#678) (#805)

* Replaced mcrypt with openssl (#678)

* Removed TODO
pull/815/head
Travis Botello 2019-11-29 23:32:09 +01:00 committed by Tim
parent 627a93cc7c
commit 7e3cbc94a9
1 changed files with 24 additions and 26 deletions

View File

@ -757,7 +757,6 @@ function psm_no_cache() {
* @return string * @return string
* @author Pavel Laupe Dvorak <pavel@pavel-dvorak.cz> * @author Pavel Laupe Dvorak <pavel@pavel-dvorak.cz>
*/ */
// TODO change to working function
function psm_password_encrypt($key, $password) function psm_password_encrypt($key, $password)
{ {
if (empty($password)) { if (empty($password)) {
@ -768,19 +767,17 @@ function psm_password_encrypt($key, $password)
throw new \InvalidArgumentException('invalid_encryption_key'); throw new \InvalidArgumentException('invalid_encryption_key');
} }
// TODO rewrite // using open ssl
$iv = mcrypt_create_iv( $cipher="AES-256-CBC";
mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), $ivlen = openssl_cipher_iv_length($cipher);
MCRYPT_DEV_URANDOM $iv = openssl_random_pseudo_bytes( $ivlen );
);
$encrypted = base64_encode( $encrypted = base64_encode(
$iv . $iv .
mcrypt_encrypt( openssl_encrypt(
MCRYPT_RIJNDAEL_128,
hash('sha256', $key, true),
$password, $password,
MCRYPT_MODE_CBC, $cipher,
hash('sha256', $key, true),
OPENSSL_RAW_DATA, // OPENSSL_ZERO_PADDING OPENSSL_RAW_DATA
$iv $iv
) )
); );
@ -806,17 +803,18 @@ function psm_password_decrypt($key, $encryptedString)
throw new \InvalidArgumentException('invalid_encryption_key'); throw new \InvalidArgumentException('invalid_encryption_key');
} }
// using open ssl
$data = base64_decode($encryptedString); $data = base64_decode($encryptedString);
$iv = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)); $cipher="AES-256-CBC";
$ivlen = openssl_cipher_iv_length($cipher);
$iv = substr($data, 0, $ivlen);
$decrypted = rtrim( $decrypted = rtrim(
mcrypt_decrypt( openssl_decrypt(
MCRYPT_RIJNDAEL_128, base64_encode(substr($data, $ivlen)),
$cipher,
hash('sha256', $key, true), hash('sha256', $key, true),
substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)), OPENSSL_ZERO_PADDING,
MCRYPT_MODE_CBC, $iv),
$iv
),
"\0" "\0"
); );