7 declare(strict_types=1);
33 $this->_randomGenerator = $this->createMock(\
Magento\Framework\Math\Random::class);
34 $deploymentConfigMock = $this->createMock(\
Magento\Framework\
App\DeploymentConfig::class);
35 $deploymentConfigMock->expects($this->any())
38 ->will($this->returnValue(self::CRYPT_KEY_1));
39 $this->_model = new \Magento\Framework\Encryption\Encryptor($this->_randomGenerator, $deploymentConfigMock);
44 $this->_randomGenerator->expects($this->never())->method(
'getRandomString');
45 $expected =
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8';
46 $actual = $this->_model->getHash(
'password');
47 $this->assertEquals($expected, $actual);
52 $this->_randomGenerator->expects($this->never())->method(
'getRandomString');
53 $expected =
'13601bda4ea78e55a07b98866d2be6be0744e3866f13c00c811cab608a28f322:salt:1';
54 $actual = $this->_model->getHash(
'password',
'salt');
55 $this->assertEquals($expected, $actual);
60 $salt =
'-----------random_salt----------';
61 $this->_randomGenerator
62 ->expects($this->once())
63 ->method(
'getRandomString')
65 ->will($this->returnValue($salt));
66 $expected =
'a1c7fc88037b70c9be84d3ad12522c7888f647915db78f42eb572008422ba2fa:' . $salt .
':1';
67 $actual = $this->_model->getHash(
'password',
true);
68 $this->assertEquals($expected, $actual);
73 $this->_randomGenerator
74 ->expects($this->once())
75 ->method(
'getRandomString')
77 ->will($this->returnValue(
'random_salt'));
78 $expected =
'4c5cab8dd00137d11258f8f87b93fd17bd94c5026fc52d3c5af911dd177a2611:random_salt:1';
79 $actual = $this->_model->getHash(
'password', 11);
80 $this->assertEquals($expected, $actual);
92 $actual = $this->_model->validateHash($password, $hash);
93 $this->assertEquals($expected, $actual);
102 [
'password',
'hash:salt:1',
false],
103 [
'password',
'67a1e09bb1f83f5007dc119c14d663aa:salt:0',
true],
104 [
'password',
'13601bda4ea78e55a07b98866d2be6be0744e3866f13c00c811cab608a28f322:salt:1',
true],
116 $deploymentConfigMock = $this->createMock(\
Magento\Framework\
App\DeploymentConfig::class);
117 $deploymentConfigMock->expects($this->any())
120 ->will($this->returnValue($key));
121 $model =
new Encryptor($this->_randomGenerator, $deploymentConfigMock);
122 $value =
'arbitrary_string';
131 return [[
null], [0], [
''], [
'0']];
141 $deploymentConfigMock = $this->createMock(\
Magento\Framework\
App\DeploymentConfig::class);
142 $deploymentConfigMock->expects($this->any())
145 ->will($this->returnValue($key));
146 $model =
new Encryptor($this->_randomGenerator, $deploymentConfigMock);
147 $value =
'arbitrary_string';
156 return [[
null], [0], [
''], [
'0']];
162 $data =
'Mares eat oats and does eat oats, but little lambs eat ivy.';
164 $actual = $this->_model->encrypt(
$data);
167 $parts = explode(
':', $actual, 3);
168 list(, , $encryptedData) = $parts;
172 $this->assertEquals(
$data, $crypt->decrypt(base64_decode((
string)$encryptedData)));
177 $message =
'Mares eat oats and does eat oats, but little lambs eat ivy.';
178 $encrypted = $this->_model->encrypt(
$message);
180 $this->assertEquals(
$message, $this->_model->decrypt($encrypted));
186 $data =
'0:2:z3a4ACpkU35W6pV692U4ueCVQP0m0v0p:' .
187 'DhEG8/uKGGq92ZusqrGb6X/9+2Ng0QZ9z2UZwljgJbs5/A3LaSnqcK0oI32yjHY49QJi+Z7q1EKu2yVqB8EMpA==';
189 $actual = $this->_model->decrypt(
$data);
192 $parts = explode(
':',
$data, 4);
193 list(, , $iv, $encrypted) = $parts;
196 $crypt =
new Crypt(self::CRYPT_KEY_1, MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC, $iv);
198 $this->assertEquals($encrypted, base64_encode($crypt->encrypt($actual)));
203 $deploymentConfigMock = $this->createMock(\
Magento\Framework\
App\DeploymentConfig::class);
204 $deploymentConfigMock->expects($this->at(0))
207 ->will($this->returnValue(self::CRYPT_KEY_1));
208 $deploymentConfigMock->expects($this->at(1))
211 ->will($this->returnValue(self::CRYPT_KEY_1 .
"\n" . self::CRYPT_KEY_2));
212 $model1 =
new Encryptor($this->_randomGenerator, $deploymentConfigMock);
217 $data =
'Mares eat oats and does eat oats, but little lambs eat ivy.';
219 $encryptedData = $model1->encrypt(
$data);
220 $decryptedData =
$model2->decrypt($encryptedData);
222 $this->assertSame(
$data, $decryptedData,
'Encryptor failed to decrypt data encrypted by old keys.');
227 $this->_model->validateKey(self::CRYPT_KEY_1);
235 $this->_model->validateKey(
'----- ');
245 '67a1e09bb1f83f5007dc119c14d663aa:salt:0'],
247 '13601bda4ea78e55a07b98866d2be6be0744e3866f13c00c811cab608a28f322:salt:1'],
249 '5f4dcc3b5aa765d61d8327deb882cf99'],
251 '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8']
265 $hash = $this->_model->getHash($password, $salt, $hashAlgo);
266 $this->assertEquals($expected, $hash);
testGetHashRandomSaltSpecifiedLength()
decryptWithEmptyKeyDataProvider()
testGetHashSpecifiedSalt()
testValidateHash($password, $hash, $expected)
encryptWithEmptyKeyDataProvider()
const HASH_VERSION_SHA256
testEncryptWithEmptyKey($key)
testEncryptDecryptNewKeyAdded()
testGetHashMustUseSpecifiedHashingAlgo($password, $salt, $hashAlgo, $expected)
testDecryptWithEmptyKey($key)
validateHashDataProvider()
useSpecifiedHashingAlgoDataProvider()
testGetHashRandomSaltDefaultLength()