Skip to content
This repository has been archived by the owner on Jun 10, 2019. It is now read-only.

Commit

Permalink
OpenSSL adapter works for all keys now if php>=7.1.18. Carrier abstra…
Browse files Browse the repository at this point in the history
…ct cast fix.
  • Loading branch information
Ivan Weiler committed Feb 2, 2018
1 parent 696ef46 commit 9eefdae
Show file tree
Hide file tree
Showing 3 changed files with 634 additions and 13 deletions.
43 changes: 32 additions & 11 deletions app/code/local/Inchoo/PHP7/Model/Crypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,37 @@ class Inchoo_PHP7_Model_Crypt
* @return Varien_Crypt_Abstract
* @throws Varien_Exception
*/
static public function factory($method = 'auto')
{
if($method == 'mcrypt' || ($method == 'auto' && function_exists('mcrypt_module_open'))) {
$crypt = new Varien_Crypt_Mcrypt();
} elseif ($method == 'openssl' || ($method == 'auto' && extension_loaded('openssl'))) {
$crypt = new Inchoo_PHP7_Model_Crypt_Openssl();
} else {
throw new Varien_Exception('Crypt adapter not available.');
}
static public function factory($method = 'auto')
{
/**
* Try to resolve best scenario:
* 1. mcrypt extension and it's not deprecated
* 2. mcrypt polyfill
* 3. openssl
*/
if($method == 'auto') {

return $crypt;
}
$hasMcrypt = extension_loaded('mcrypt');
$hasMcryptPolyfill = !$hasMcrypt && function_exists('mcrypt_module_open');

if(($hasMcrypt && version_compare(PHP_VERSION, '7.1.0', '<')) || $hasMcryptPolyfill) {
$method = 'mcrypt';
} elseif (extension_loaded('openssl')) {
$method = 'openssl';
}
}

switch ($method) {
case 'openssl':
$crypt = new Inchoo_PHP7_Model_Crypt_Openssl();
break;
case 'mcrypt':
$crypt = new Varien_Crypt_Mcrypt();
break;
default:
throw new Varien_Exception( 'Crypt adapter not available.' );
}

return $crypt;
}
}
14 changes: 12 additions & 2 deletions app/code/local/Inchoo/PHP7/Model/Crypt/Openssl.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,16 @@ public function encrypt($data)
$data .= str_repeat("\0", $blockSize - $modus);
}

$openSslOptions = OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING;
if(defined('OPENSSL_DONT_ZERO_PAD_KEY')) {
$openSslOptions |= OPENSSL_DONT_ZERO_PAD_KEY;
}

$encryptedData = openssl_encrypt(
$data,
$this->getOpensslMethod(),
$this->getKey(),
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING,
$openSslOptions,
$this->getInitVector()
);

Expand Down Expand Up @@ -153,11 +158,16 @@ public function decrypt($data)
return $data;
}

$openSslOptions = OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING;
if(defined('OPENSSL_DONT_ZERO_PAD_KEY')) {
$openSslOptions |= OPENSSL_DONT_ZERO_PAD_KEY;
}

$decryptedData = openssl_decrypt(
$data,
$this->getOpensslMethod(),
$this->getKey(),
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING,
$openSslOptions,
$this->getInitVector()
);

Expand Down
Loading

0 comments on commit 9eefdae

Please sign in to comment.