/** * {@inheritdoc} */ public function handle(Record $recordToHandle, Record $oppositeRecord) { $value = $recordToHandle->getValue('value'); if (!$value) { return; } $this->validate($recordToHandle); $parts = explode(':', $value, 4); $partsCount = count($parts); if ($partsCount == 4) { $initVector = $parts[2]; $encryptedValue = $parts[3]; $mode = MCRYPT_MODE_CBC; $cypher = MCRYPT_RIJNDAEL_128; } else { $initVector = false; $encryptedValue = $value; $mode = MCRYPT_MODE_ECB; $cypher = MCRYPT_BLOWFISH; } $crypt = $this->cryptFactory->create(['key' => $this->cryptKey, 'cipher' => $cypher, 'mode' => $mode, 'initVector' => $initVector]); $decryptedValue = trim($crypt->decrypt(base64_decode((string) $encryptedValue))); $encodedValue = $this->encryptor->encrypt($decryptedValue); $recordToHandle->setValue('value', $encodedValue); }
/** * @param string $value * @param string $expected * * @dataProvider encryptDataProvider */ public function testEncrypt($value, $expected) { $crypt = $this->getMock('Magento\\Framework\\Encryption\\Crypt', array(), array(), '', false); $this->_cryptFactory->expects($this->once())->method('create')->will($this->returnValue($crypt)); $crypt->expects($this->once())->method('encrypt')->with($value)->will($this->returnArgument(0)); $actual = $this->_model->encrypt($value); $this->assertEquals($expected, $actual); }
public function testEncrypt() { // sample data to encrypt $data = 'Mares eat oats and does eat oats, but little lambs eat ivy.'; $actual = $this->_model->encrypt($data); // Extract the initialization vector and encrypted data $parts = explode(':', $actual, 4); list(, , $iv, $encryptedData) = $parts; // Decrypt returned data with RIJNDAEL_256 cipher, cbc mode $crypt = new Crypt('cryptKey', MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC, $iv); // Verify decrypted matches original data $this->assertEquals($data, $crypt->decrypt(base64_decode((string) $encryptedData))); }
public function testEncryptDecryptNewKeyAdded() { $deploymentConfigMock = $this->getMock('\\Magento\\Framework\\App\\DeploymentConfig', [], [], '', false); $deploymentConfigMock->expects($this->at(0))->method('get')->with(Encryptor::PARAM_CRYPT_KEY)->will($this->returnValue("cryptKey1")); $deploymentConfigMock->expects($this->at(1))->method('get')->with(Encryptor::PARAM_CRYPT_KEY)->will($this->returnValue("cryptKey1\ncryptKey2")); $model1 = new Encryptor($this->_randomGenerator, $deploymentConfigMock); // simulate an encryption key is being added $model2 = new Encryptor($this->_randomGenerator, $deploymentConfigMock); // sample data to encrypt $data = 'Mares eat oats and does eat oats, but little lambs eat ivy.'; // encrypt with old key $encryptedData = $model1->encrypt($data); $decryptedData = $model2->decrypt($encryptedData); $this->assertSame($data, $decryptedData, 'Encryptor failed to decrypt data encrypted by old keys.'); }
public function testEncryptDecrypt() { $this->assertEquals('', $this->_model->decrypt($this->_model->encrypt(''))); $this->assertEquals('test', $this->_model->decrypt($this->_model->encrypt('test'))); }