/**
  * {@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);
 }
Ejemplo n.º 2
0
 /**
  * Instantiate crypt model
  *
  * @param string|null $key NULL value means usage of the default key specified on constructor
  * @return \Magento\Framework\Encryption\Crypt
  */
 protected function _getCrypt($key = null)
 {
     if ($key === null) {
         if (!$this->_crypt) {
             $this->_crypt = $this->_cryptFactory->create(array('key' => $this->_cryptKey));
         }
         return $this->_crypt;
     } else {
         return $this->_cryptFactory->create(array('key' => $key));
     }
 }
 /**
  * @param array $attributeData
  * @param array $expected
  *
  * @dataProvider dataProviderEncryptionData
  * @return void
  */
 public function testHandle($attributeData, $expected)
 {
     $fieldName = 'value';
     list($key, $dbValue, $initVector, $encryptedValue, $cypher, $mode) = array_values($attributeData);
     list($decryptedValue, $newValue) = array_values($expected);
     /** @var \Migration\ResourceModel\Record|\PHPUnit_Framework_MockObject_MockObject $recordToHandle */
     $recordToHandle = $this->getMock('Migration\\ResourceModel\\Record', ['getValue', 'setValue', 'getFields'], [], '', false);
     $recordToHandle->expects($this->once())->method('getValue')->with($fieldName)->willReturn($dbValue);
     $recordToHandle->expects($this->once())->method('setValue')->with($fieldName, $newValue);
     $recordToHandle->expects($this->once())->method('getFields')->will($this->returnValue([$fieldName]));
     $oppositeRecord = $this->getMockBuilder('Migration\\ResourceModel\\Record')->disableOriginalConstructor()->getMock();
     $crypt = $this->getMock('\\Magento\\Framework\\Encryption\\Crypt', ['decrypt'], [$key, $cypher, $mode, $initVector], '', true);
     $crypt->expects($this->once())->method('decrypt')->with(base64_decode((string) $encryptedValue))->willReturn($decryptedValue);
     $this->configReader->expects($this->once())->method('getOption')->with(self::CRYPT_KEY)->will($this->returnValue($key));
     $this->cryptFactory->expects($this->once())->method('create')->with(['key' => $key, 'cipher' => $cypher, 'mode' => $mode, 'initVector' => $initVector])->will($this->returnValue($crypt));
     $this->encryptor->expects($this->once())->method('encrypt')->with($decryptedValue)->willReturn($newValue);
     $handler = new \Migration\Handler\Settings\Encrypt($this->encryptor, $this->cryptFactory, $this->configReader);
     $handler->setField($fieldName);
     $handler->handle($recordToHandle, $oppositeRecord);
 }