Skip to content

Commit

Permalink
Merge pull request #1 from mantas-gudelevicius-hiccup/mcrypt_to_openssl
Browse files Browse the repository at this point in the history
Migrating from Mcrypt to OpenSSL encryption library
  • Loading branch information
gusdecool committed Jun 4, 2018
2 parents 0093706 + a341d25 commit 337ba2c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
4 changes: 2 additions & 2 deletions composer.json
@@ -1,6 +1,5 @@
{
"name": "gusdecool/cryptography",
"version": "1.0.0",
"license": "MIT",
"description": "Cryptography library",
"type": "library",
Expand All @@ -21,7 +20,8 @@
}
},
"require": {
"ext-mcrypt": "*"
"php": "~7.0",
"ext-openssl": "*"
},
"require-dev": {
"phpunit/phpunit": "4.8.*"
Expand Down
19 changes: 8 additions & 11 deletions src/Encryption/AesEncryption.php
Expand Up @@ -15,15 +15,13 @@ class AesEncryption
# Properties
#------------------------------------------------------------------------------------------------------

/**
* @var string
*/
private $secureKey;
const CHIPPER = 'AES256';
const IV = '\'Nl6P0*3\'Nl6P0*3';

/**
* @var string
*/
private $iv;
private $secureKey;

#------------------------------------------------------------------------------------------------------
# Magic methods
Expand All @@ -34,10 +32,9 @@ class AesEncryption
*
* @param string $aesKey security key to use for encryption and decryption
*/
public function __construct($aesKey)
public function __construct(string $aesKey)
{
$this->secureKey = hash('sha256', $aesKey, true);
$this->iv = mcrypt_create_iv(32);
}

#------------------------------------------------------------------------------------------------------
Expand All @@ -50,10 +47,10 @@ public function __construct($aesKey)
* @param string $input
* @return string
*/
public function encrypt($input)
public function encrypt(string $input): string
{
return base64_encode(
mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->secureKey, $input, MCRYPT_MODE_ECB, $this->iv)
openssl_encrypt($input, self::CHIPPER, $this->secureKey, 0, self::IV)
);
}

Expand All @@ -63,10 +60,10 @@ public function encrypt($input)
* @param string $input
* @return string
*/
public function decrypt($input)
public function decrypt(string $input): string
{
return trim(
mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->secureKey, base64_decode($input), MCRYPT_MODE_ECB, $this->iv)
openssl_decrypt(base64_decode($input), self::CHIPPER, $this->secureKey, 0, self::IV)
);
}
}
4 changes: 2 additions & 2 deletions test/Encryption/AesEncryptionTest.php
Expand Up @@ -39,14 +39,14 @@ protected function setUp()
*/
public function testEncrypt()
{
$this->assertEquals('+P24cXIcklfKOHOuFRkIE3WprB6W11NRZnzg8SJRGoM=', $this->encryption->encrypt('alpha'));
$this->assertEquals('VHExMGMweUh4anRGZjlKbnphK2ZrZz09', $this->encryption->encrypt('alpha'));
}

/**
* @group unit
*/
public function testDecrypt()
{
$this->assertEquals('alpha', $this->encryption->decrypt('+P24cXIcklfKOHOuFRkIE3WprB6W11NRZnzg8SJRGoM='));
$this->assertEquals('alpha', $this->encryption->decrypt('VHExMGMweUh4anRGZjlKbnphK2ZrZz09'));
}
}

0 comments on commit 337ba2c

Please sign in to comment.