Ejemplo n.º 1
0
 /**
  * Decrypt
  *
  * @param  string $data
  * @return string|boolean
  * @throws Exception\InvalidArgumentException
  */
 public function decrypt($data)
 {
     if (!is_string($data)) {
         throw new Exception\InvalidArgumentException('The data to decrypt must be a string');
     }
     if ('' === $data) {
         throw new Exception\InvalidArgumentException('The data to decrypt cannot be empty');
     }
     if (empty($this->key)) {
         throw new Exception\InvalidArgumentException('No key specified for the decryption');
     }
     if (empty($this->cipher)) {
         throw new Exception\InvalidArgumentException('No symmetric cipher specified');
     }
     $hmacSize = Hmac::getOutputSize($this->hash);
     $hmac = substr($data, 0, $hmacSize);
     $ciphertext = substr($data, $hmacSize);
     if (!$this->binaryOutput) {
         $ciphertext = base64_decode($ciphertext);
     }
     $iv = substr($ciphertext, 0, $this->cipher->getSaltSize());
     $keySize = $this->cipher->getKeySize();
     // generate the encryption key and the HMAC key for the authentication
     $hash = Pbkdf2::calc(self::KEY_DERIV_HMAC, $this->getKey(), $iv, $this->keyIteration, $keySize * 2);
     // set the decryption key
     $this->cipher->setKey(substr($hash, 0, $keySize));
     // set the key for HMAC
     $keyHmac = substr($hash, $keySize);
     $hmacNew = Hmac::compute($keyHmac, $this->hash, $this->cipher->getAlgorithm() . $ciphertext);
     if (!Utils::compareStrings($hmacNew, $hmac)) {
         return false;
     }
     return $this->cipher->decrypt($ciphertext);
 }
Ejemplo n.º 2
0
 /**
  * Decrypt a file
  *
  * @param  string                             $fileIn
  * @param  string                             $fileOut
  * @param  bool                               $compress
  * @return bool
  * @throws Exception\InvalidArgumentException
  */
 public function decrypt($fileIn, $fileOut)
 {
     $this->checkFileInOut($fileIn, $fileOut);
     if (empty($this->key)) {
         throw new Exception\InvalidArgumentException('No key specified for decryption');
     }
     $read = fopen($fileIn, "r");
     $write = fopen($fileOut, "w");
     $hmacRead = fread($read, Hmac::getOutputSize($this->getHashAlgorithm()));
     $iv = fread($read, $this->cipher->getSaltSize());
     $tot = filesize($fileIn);
     $hmac = $iv;
     $size = strlen($iv) + strlen($hmacRead);
     $keys = Pbkdf2::calc($this->getPbkdf2HashAlgorithm(), $this->getKey(), $iv, $this->getKeyIteration(), $this->cipher->getKeySize() * 2);
     $padding = $this->cipher->getPadding();
     $this->cipher->setPadding(new Symmetric\Padding\NoPadding());
     $this->cipher->setKey(substr($keys, 0, $this->cipher->getKeySize()));
     $this->cipher->setMode('cbc');
     $blockSize = $this->cipher->getBlockSize();
     $hashAlgo = $this->getHashAlgorithm();
     $algorithm = $this->cipher->getAlgorithm();
     $saltSize = $this->cipher->getSaltSize();
     $keyHmac = substr($keys, $this->cipher->getKeySize());
     while ($data = fread($read, self::BUFFER_SIZE)) {
         $size += strlen($data);
         // Unpadding if last block
         if ($size + $blockSize >= $tot) {
             $this->cipher->setPadding($padding);
             $data .= fread($read, $blockSize);
         }
         $result = $this->cipher->decrypt($iv . $data);
         $hmac = Hmac::compute($keyHmac, $hashAlgo, $algorithm . $hmac . $data);
         $iv = substr($data, -1 * $saltSize);
         if (fwrite($write, $result) !== strlen($result)) {
             return false;
         }
     }
     fclose($write);
     fclose($read);
     // check for data integrity
     if (!Utils::compareStrings($hmac, $hmacRead)) {
         unlink($fileOut);
         return false;
     }
     return true;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function verify($plain, $hashed)
 {
     return hash_equals($this->symmetric->encrypt($plain), $hashed);
 }
Ejemplo n.º 4
0
 public function testYouCanDecryptUsingZendAbstractMethod()
 {
     $this->symmetricAdapter->expects($this->once())->method('decrypt')->will($this->returnValue('foo'));
     $this->symmetricAdapter->expects($this->once())->method('getAlgorithm')->will($this->returnValue('foo'));
     $this->assertEquals('foo', $this->sut->decrypt('84c25d9d9211932f7a69c3e84a090bade57b8c35dd6f8e1a66801dd889632ee8Zm9v', 'fudge'));
 }
 /**
  *
  */
 protected function updateSalt()
 {
     $this->crypt->setSalt(Rand::getBytes($this->crypt->getSaltSize(), $this->getRequireStrongRandomGenerator()));
 }