Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initialization vector to Encryption class #7297

Open
wants to merge 1 commit into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Added initialization vector to encryption class

This is a drop-in replacement that fixes the issue where openssl_encrypt emits an E_WARNING level error if an empty value is passed in via the iv parameter (https://www.php.net/manual/en/function.openssl-encrypt.php).
  • Loading branch information...
garudacrafts committed Mar 29, 2019
commit 091e686fbb61914fc22a42bfa0eab9ac85c6074d
@@ -11,6 +11,16 @@
* Encryption class
*/
final class Encryption {
private $cipher;
private $iv;
private $iv_length;
public function __construct() {
$this->cipher = 'aes-128-cbc';
$this->iv_length = openssl_cipher_iv_length($this->cipher);
$this->iv = openssl_random_pseudo_bytes($this->iv_length);
}
/**
*
*
@@ -20,7 +30,7 @@ final class Encryption {
* @return string
*/
public function encrypt($key, $value) {
return strtr(base64_encode(openssl_encrypt($value, 'aes-128-cbc', hash('sha256', $key, true))), '+/=', '-_,');
return strtr(base64_encode($this->iv . openssl_encrypt($value, $this->cipher, hash('sha256', $key, true), 0, $this->iv)), '+/=', '-_,');
}
/**
@@ -32,6 +42,10 @@ public function encrypt($key, $value) {
* @return string
*/
public function decrypt($key, $value) {
return trim(openssl_decrypt(base64_decode(strtr($value, '-_,', '+/=')), 'aes-128-cbc', hash('sha256', $key, true)));
$mixed = base64_decode(strtr($value, '-_,', '+/='));
$iv = substr($mixed, 0, $this->iv_length);
$encrypted = substr($mixed, $this->iv_length);
return trim(openssl_decrypt($encrypted, $this->cipher, hash('sha256', $key, true), 0, $iv));
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.