Skip to content

Commit 2a42199

Browse files
committed
Change return types of getCrypt and validateKey
`Magento\Framework\Encryption\Encryptor::validateKey` - Now returns `void` instead of an instance of `\Magento\Framework\Encryption\Crypt`. `Magento\Framework\Encryption\Encryptor::getCrypt` - Scope was changed from `protected` to `private` and return value is now an instance of `Magento\Framework\Encryption\Adapter\EncryptionAdapterInterface`
1 parent bb72792 commit 2a42199

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

dev/tests/integration/testsuite/Magento/Framework/Encryption/ModelTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ public function testEncryptDecrypt2()
4242
public function testValidateKey()
4343
{
4444
$validKey = md5(uniqid());
45-
$this->assertInstanceOf(
46-
\Magento\Framework\Encryption\Adapter\EncryptionAdapterInterface::class,
47-
$this->_model->validateKey($validKey)
48-
);
45+
$this->_model->validateKey($validKey);
46+
}
47+
48+
/**
49+
* @expectedException \Exception
50+
*/
51+
public function testValidateKeyInvalid()
52+
{
53+
$invalidKey = '---- ';
54+
$this->_model->validateKey($invalidKey);
4955
}
5056

5157
public function testGetValidateHash()

lib/internal/Magento/Framework/Encryption/Adapter/Mcrypt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function getMode(): string
114114
*
115115
* @return string
116116
*/
117-
public function getInitVector(): string
117+
public function getInitVector(): ?string
118118
{
119119
return $this->initVector;
120120
}

lib/internal/Magento/Framework/Encryption/Encryptor.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ public function decrypt($data)
298298
$parts = explode(':', $data, 4);
299299
$partsCount = count($parts);
300300

301-
$initVector = false;
301+
$initVector = null;
302302
// specified key, specified crypt, specified iv
303303
if (4 === $partsCount) {
304304
list($keyVersion, $cryptVersion, $iv, $data) = $parts;
305-
$initVector = $iv ? $iv : false;
305+
$initVector = $iv ? $iv : null;
306306
$keyVersion = (int)$keyVersion;
307307
$cryptVersion = self::CIPHER_RIJNDAEL_256;
308308
// specified key, specified crypt
@@ -337,18 +337,16 @@ public function decrypt($data)
337337
}
338338

339339
/**
340-
* Return crypt model, instantiate if it is empty
340+
* Validate key contains only allowed characters
341341
*
342342
* @param string|null $key NULL value means usage of the default key specified on constructor
343-
* @return EncryptionAdapterInterface
344343
* @throws \Exception
345344
*/
346345
public function validateKey($key)
347346
{
348347
if (preg_match('/\s/s', $key)) {
349348
throw new \Exception((string)new \Magento\Framework\Phrase('The encryption key format is invalid.'));
350349
}
351-
return $this->getCrypt($key);
352350
}
353351

354352
/**
@@ -383,12 +381,15 @@ public function exportKeys()
383381
*
384382
* @param string $key
385383
* @param int $cipherVersion
386-
* @param bool $initVector
384+
* @param string $initVector
387385
* @return EncryptionAdapterInterface|null
388386
* @throws \Exception
389387
*/
390-
protected function getCrypt($key = null, $cipherVersion = null, $initVector = true)
391-
{
388+
private function getCrypt(
389+
string $key = null,
390+
int $cipherVersion = null,
391+
string $initVector = null
392+
): ?EncryptionAdapterInterface {
392393
if (null === $key && null === $cipherVersion) {
393394
$cipherVersion = self::CIPHER_RIJNDAEL_256;
394395
}

lib/internal/Magento/Framework/Encryption/Test/Unit/EncryptorTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,15 @@ public function testEncryptDecryptNewKeyAdded()
216216

217217
public function testValidateKey()
218218
{
219-
$actual = $this->_model->validateKey(self::CRYPT_KEY_1);
220-
$crypt = new Sodium(self::CRYPT_KEY_1);
221-
$expectedEncryptedData = base64_encode($crypt->encrypt('data'));
222-
$actualEncryptedData = base64_encode($actual->encrypt('data'));
223-
$this->assertNotEquals($expectedEncryptedData, $actualEncryptedData);
224-
$this->assertEquals($crypt->decrypt($expectedEncryptedData), $actual->decrypt($actualEncryptedData));
219+
$this->_model->validateKey(self::CRYPT_KEY_1);
220+
}
221+
222+
/**
223+
* @expectedException \Exception
224+
*/
225+
public function testValidateKeyInvalid()
226+
{
227+
$this->_model->validateKey('----- ');
225228
}
226229

227230
public function testUseSpecifiedHashingAlgoDataProvider()

0 commit comments

Comments
 (0)