Skip to content

Commit ce5c202

Browse files
Replace mcrypt with openssl as the default extension used for encryption. Fixes support for PHP 7.2+. Refs #129, #449.
1 parent 51a0c5b commit ce5c202

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

app/code/core/Mage/Install/etc/install.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080
<spl/>
8181
<dom/>
8282
<simplexml/>
83-
<mcrypt/>
83+
<openssl/>
84+
<!--<mcrypt/> No longer supported, use Openssl instead -->
8485
<hash/>
8586
<curl/>
8687
<iconv/>

lib/Varien/Crypt.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,19 @@ class Varien_Crypt
3939
*
4040
* @param string $method
4141
* @return Varien_Crypt_Abstract
42+
* @throws Exception
4243
*/
43-
static public function factory($method='mcrypt')
44+
static public function factory($method = NULL)
4445
{
46+
if ($method == NULL) {
47+
if (function_exists('openssl_encrypt')) {
48+
$method = 'openssl';
49+
} else if (function_exists('mcrypt_module_open')) {
50+
$method = 'mcrypt'; // Not supported after PHP 7.2
51+
} else {
52+
throw new Exception('No supported encryption library is available. Install Openssl (recommended) or Mcrypt extension.');
53+
}
54+
}
4555
$uc = str_replace(' ','_',ucwords(str_replace('_',' ',$method)));
4656
$className = 'Varien_Crypt_'.$uc;
4757
return new $className;

lib/Varien/Crypt/Openssl.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Magento
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Open Software License (OSL 3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* http://opensource.org/licenses/osl-3.0.php
11+
* If you did not receive a copy of the license and are unable to
12+
* obtain it through the world-wide-web, please send an email
13+
* to license@magentocommerce.com so we can send you a copy immediately.
14+
*
15+
* DISCLAIMER
16+
*
17+
* Do not edit or add to this file if you wish to upgrade Magento to newer
18+
* versions in the future. If you wish to customize Magento for your
19+
* needs please refer to http://www.magentocommerce.com for more information.
20+
*
21+
* @category Varien
22+
* @package Varien_Crypt
23+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
24+
*/
25+
26+
27+
/**
28+
* Openssl plugin
29+
*
30+
* @category Varien
31+
* @package Varien_Crypt
32+
*/
33+
class Varien_Crypt_Openssl extends Varien_Crypt_Abstract
34+
{
35+
36+
private $_key;
37+
38+
/**
39+
* Initialize encryption module
40+
*
41+
* @param string $key cipher private key
42+
* @return $this
43+
*/
44+
public function init($key)
45+
{
46+
$this->_key = $key;
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* Encrypt data
53+
*
54+
* @param string $data source string
55+
* @return string
56+
*/
57+
public function encrypt($data)
58+
{
59+
return openssl_encrypt($data, 'bf-ecb', $this->_key, OPENSSL_RAW_DATA);
60+
}
61+
62+
/**
63+
* Decrypt data
64+
*
65+
* @param string $data encrypted string
66+
* @return string
67+
*/
68+
public function decrypt($data)
69+
{
70+
return openssl_decrypt($data, 'bf-ecb', $this->_key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
71+
}
72+
73+
}

0 commit comments

Comments
 (0)