Skip to content

Commit 640beb7

Browse files
committed
Switch to OpenSSL (mcrypt deprecated in PHP 7.1)
1 parent 260bedf commit 640beb7

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"squizlabs/php_codesniffer": "^3.5"
3939
},
4040
"suggest": {
41-
"ext-mcrypt": "Required for hashing functions to check message signatures"
41+
"ext-openssl": "Required for hashing functions to check message signatures"
4242
},
4343
"extra": {
4444
"branch-alias": {

src/Message/Security.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ protected function encryptMessage($message, $key)
5858
$iv = implode(array_map('chr', array(0, 0, 0, 0, 0, 0, 0, 0)));
5959

6060
if ($this->hasValidEncryptionMethod()) {
61-
$ciphertext = mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv);
61+
// OpenSSL needs to manually pad $message length to be mod 8 = 0; OPENSSL_ZERO_PADDING option doens't work
62+
if (strlen($message) % 8) {
63+
$message = str_pad($message, strlen($message) + 8 - strlen($message) % 8, "\0");
64+
}
65+
$ciphertext = openssl_encrypt($message, 'des-ede3-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
6266
} else {
6367
throw new RuntimeException('No valid encryption extension installed');
6468
}
@@ -73,7 +77,7 @@ protected function encryptMessage($message, $key)
7377
*/
7478
public function hasValidEncryptionMethod()
7579
{
76-
return extension_loaded('mcrypt') && function_exists('mcrypt_encrypt');
80+
return extension_loaded('openssl') && function_exists('openssl_encrypt');
7781
}
7882

7983
/**

tests/Message/SecurityTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public function testDecodeMerchantParameters()
3838
}
3939

4040
/**
41-
* Confirm that the mcrypt extension is loaded and we have the appropriate method
41+
* Confirm that the openssl extension is loaded and we have the appropriate method
4242
*
4343
* Checks are split out in case only one is failing, rather than the blanket true/false for both
4444
*/
4545
public function testHasValidEncryptionMethod()
4646
{
47-
$this->assertTrue(extension_loaded('mcrypt'));
48-
$this->assertTrue(function_exists('mcrypt_encrypt'));
47+
$this->assertTrue(extension_loaded('openssl'));
48+
$this->assertTrue(function_exists('openssl_encrypt'));
4949
$this->assertTrue($this->security->hasValidEncryptionMethod());
5050
}
5151

0 commit comments

Comments
 (0)