Replaced mcrypt with openssl (#678)

pull/805/head
Travis Botello 2019-10-27 10:05:57 +01:00 committed by GitHub
parent 627a93cc7c
commit 4123749bab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 25 deletions

View File

@ -768,21 +768,19 @@ 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, $password,
hash('sha256', $key, true), $cipher,
$password, hash('sha256', $key, true),
MCRYPT_MODE_CBC, OPENSSL_RAW_DATA, // OPENSSL_ZERO_PADDING OPENSSL_RAW_DATA
$iv $iv
) )
); );
return $encrypted; return $encrypted;
@ -806,20 +804,21 @@ 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);
$decrypted = rtrim( $iv = substr($data, 0, $ivlen);
mcrypt_decrypt( $decrypted = rtrim(
MCRYPT_RIJNDAEL_128, openssl_decrypt(
hash('sha256', $key, true), base64_encode(substr($data, $ivlen)),
substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)), $cipher,
MCRYPT_MODE_CBC, hash('sha256', $key, true),
$iv OPENSSL_ZERO_PADDING,
), $iv),
"\0" "\0"
); );
return $decrypted; return $decrypted;
} }