Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EncryptorTest.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
15 
16 class EncryptorTest extends \PHPUnit\Framework\TestCase
17 {
18  const CRYPT_KEY_1 = 'g9mY9KLrcuAVJfsmVUSRkKFLDdUPVkaZ';
19  const CRYPT_KEY_2 = '7wEjmrliuqZQ1NQsndSa8C8WHvddeEbN';
20 
24  protected $_model;
25 
29  protected $_randomGenerator;
30 
31  protected function setUp()
32  {
33  $this->_randomGenerator = $this->createMock(\Magento\Framework\Math\Random::class);
34  $deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
35  $deploymentConfigMock->expects($this->any())
36  ->method('get')
38  ->will($this->returnValue(self::CRYPT_KEY_1));
39  $this->_model = new \Magento\Framework\Encryption\Encryptor($this->_randomGenerator, $deploymentConfigMock);
40  }
41 
42  public function testGetHashNoSalt()
43  {
44  $this->_randomGenerator->expects($this->never())->method('getRandomString');
45  $expected = '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8';
46  $actual = $this->_model->getHash('password');
47  $this->assertEquals($expected, $actual);
48  }
49 
50  public function testGetHashSpecifiedSalt()
51  {
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);
56  }
57 
59  {
60  $salt = '-----------random_salt----------';
61  $this->_randomGenerator
62  ->expects($this->once())
63  ->method('getRandomString')
64  ->with(32)
65  ->will($this->returnValue($salt));
66  $expected = 'a1c7fc88037b70c9be84d3ad12522c7888f647915db78f42eb572008422ba2fa:' . $salt . ':1';
67  $actual = $this->_model->getHash('password', true);
68  $this->assertEquals($expected, $actual);
69  }
70 
72  {
73  $this->_randomGenerator
74  ->expects($this->once())
75  ->method('getRandomString')
76  ->with(11)
77  ->will($this->returnValue('random_salt'));
78  $expected = '4c5cab8dd00137d11258f8f87b93fd17bd94c5026fc52d3c5af911dd177a2611:random_salt:1';
79  $actual = $this->_model->getHash('password', 11);
80  $this->assertEquals($expected, $actual);
81  }
82 
90  public function testValidateHash($password, $hash, $expected)
91  {
92  $actual = $this->_model->validateHash($password, $hash);
93  $this->assertEquals($expected, $actual);
94  }
95 
99  public function validateHashDataProvider()
100  {
101  return [
102  ['password', 'hash:salt:1', false],
103  ['password', '67a1e09bb1f83f5007dc119c14d663aa:salt:0', true],
104  ['password', '13601bda4ea78e55a07b98866d2be6be0744e3866f13c00c811cab608a28f322:salt:1', true],
105  ];
106  }
107 
114  public function testEncryptWithEmptyKey($key)
115  {
116  $deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
117  $deploymentConfigMock->expects($this->any())
118  ->method('get')
120  ->will($this->returnValue($key));
121  $model = new Encryptor($this->_randomGenerator, $deploymentConfigMock);
122  $value = 'arbitrary_string';
123  $this->assertEquals($value, $model->encrypt($value));
124  }
125 
130  {
131  return [[null], [0], [''], ['0']];
132  }
133 
139  public function testDecryptWithEmptyKey($key)
140  {
141  $deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
142  $deploymentConfigMock->expects($this->any())
143  ->method('get')
145  ->will($this->returnValue($key));
146  $model = new Encryptor($this->_randomGenerator, $deploymentConfigMock);
147  $value = 'arbitrary_string';
148  $this->assertEquals('', $model->decrypt($value));
149  }
150 
155  {
156  return [[null], [0], [''], ['0']];
157  }
158 
159  public function testEncrypt()
160  {
161  // sample data to encrypt
162  $data = 'Mares eat oats and does eat oats, but little lambs eat ivy.';
163 
164  $actual = $this->_model->encrypt($data);
165 
166  // Extract the initialization vector and encrypted data
167  $parts = explode(':', $actual, 3);
168  list(, , $encryptedData) = $parts;
169 
170  $crypt = new SodiumChachaIetf(self::CRYPT_KEY_1);
171  // Verify decrypted matches original data
172  $this->assertEquals($data, $crypt->decrypt(base64_decode((string)$encryptedData)));
173  }
174 
175  public function testDecrypt()
176  {
177  $message = 'Mares eat oats and does eat oats, but little lambs eat ivy.';
178  $encrypted = $this->_model->encrypt($message);
179 
180  $this->assertEquals($message, $this->_model->decrypt($encrypted));
181  }
182 
183  public function testLegacyDecrypt()
184  {
185  // sample data to encrypt
186  $data = '0:2:z3a4ACpkU35W6pV692U4ueCVQP0m0v0p:' .
187  'DhEG8/uKGGq92ZusqrGb6X/9+2Ng0QZ9z2UZwljgJbs5/A3LaSnqcK0oI32yjHY49QJi+Z7q1EKu2yVqB8EMpA==';
188 
189  $actual = $this->_model->decrypt($data);
190 
191  // Extract the initialization vector and encrypted data
192  $parts = explode(':', $data, 4);
193  list(, , $iv, $encrypted) = $parts;
194 
195  // Decrypt returned data with RIJNDAEL_256 cipher, cbc mode
196  $crypt = new Crypt(self::CRYPT_KEY_1, MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC, $iv);
197  // Verify decrypted matches original data
198  $this->assertEquals($encrypted, base64_encode($crypt->encrypt($actual)));
199  }
200 
202  {
203  $deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
204  $deploymentConfigMock->expects($this->at(0))
205  ->method('get')
207  ->will($this->returnValue(self::CRYPT_KEY_1));
208  $deploymentConfigMock->expects($this->at(1))
209  ->method('get')
211  ->will($this->returnValue(self::CRYPT_KEY_1 . "\n" . self::CRYPT_KEY_2));
212  $model1 = new Encryptor($this->_randomGenerator, $deploymentConfigMock);
213  // simulate an encryption key is being added
214  $model2 = new Encryptor($this->_randomGenerator, $deploymentConfigMock);
215 
216  // sample data to encrypt
217  $data = 'Mares eat oats and does eat oats, but little lambs eat ivy.';
218  // encrypt with old key
219  $encryptedData = $model1->encrypt($data);
220  $decryptedData = $model2->decrypt($encryptedData);
221 
222  $this->assertSame($data, $decryptedData, 'Encryptor failed to decrypt data encrypted by old keys.');
223  }
224 
225  public function testValidateKey()
226  {
227  $this->_model->validateKey(self::CRYPT_KEY_1);
228  }
229 
233  public function testValidateKeyInvalid()
234  {
235  $this->_model->validateKey('----- ');
236  }
237 
242  {
243  return [
244  ['password', 'salt', Encryptor::HASH_VERSION_MD5,
245  '67a1e09bb1f83f5007dc119c14d663aa:salt:0'],
246  ['password', 'salt', Encryptor::HASH_VERSION_SHA256,
247  '13601bda4ea78e55a07b98866d2be6be0744e3866f13c00c811cab608a28f322:salt:1'],
248  ['password', false, Encryptor::HASH_VERSION_MD5,
249  '5f4dcc3b5aa765d61d8327deb882cf99'],
250  ['password', false, Encryptor::HASH_VERSION_SHA256,
251  '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8']
252  ];
253  }
254 
263  public function testGetHashMustUseSpecifiedHashingAlgo($password, $salt, $hashAlgo, $expected)
264  {
265  $hash = $this->_model->getHash($password, $salt, $hashAlgo);
266  $this->assertEquals($expected, $hash);
267  }
268 }
$message
$value
Definition: gender.phtml:16
testGetHashMustUseSpecifiedHashingAlgo($password, $salt, $hashAlgo, $expected)