Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
58 changes: 46 additions & 12 deletions src/BlockCipher.php
Expand Up @@ -20,7 +20,12 @@
*/
class BlockCipher
{
const KEY_DERIV_HMAC = 'sha256';
/**
* Hash algorithm for Pbkdf2
*
* @var string
*/
protected $pbkdf2Hash = 'sha256';

/**
* Symmetric cipher
Expand All @@ -37,7 +42,7 @@ class BlockCipher
protected static $symmetricPlugins = null;

/**
* Hash algorithm fot HMAC
* Hash algorithm for HMAC
*
* @var string
*/
Expand Down Expand Up @@ -74,7 +79,7 @@ class BlockCipher
/**
* Constructor
*
* @param SymmetricInterface $cipher
* @param SymmetricInterface $cipher
*/
public function __construct(SymmetricInterface $cipher)
{
Expand Down Expand Up @@ -162,7 +167,7 @@ public function getCipher()
/**
* Set the number of iterations for Pbkdf2
*
* @param int $num
* @param int $num
* @return BlockCipher
*/
public function setKeyIteration($num)
Expand All @@ -185,7 +190,7 @@ public function getKeyIteration()
/**
* Set the salt (IV)
*
* @param string $salt
* @param string $salt
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
Expand Down Expand Up @@ -224,7 +229,7 @@ public function getOriginalSalt()
/**
* Enable/disable the binary output
*
* @param bool $value
* @param bool $value
* @return BlockCipher
*/
public function setBinaryOutput($value)
Expand Down Expand Up @@ -274,7 +279,7 @@ public function getKey()
/**
* Set algorithm of the symmetric cipher
*
* @param string $algo
* @param string $algo
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
Expand Down Expand Up @@ -323,7 +328,7 @@ public function getCipherSupportedAlgorithms()
/**
* Set the hash algorithm for HMAC authentication
*
* @param string $hash
* @param string $hash
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -349,10 +354,39 @@ public function getHashAlgorithm()
return $this->hash;
}

/**
* Set the hash algorithm for the Pbkdf2
*
* @param string $hash
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
public function setPbkdf2HashAlgorithm($hash)
{
if (!Hash::isSupported($hash)) {
throw new Exception\InvalidArgumentException(
"The specified hash algorithm '{$hash}' is not supported by Zend\Crypt\Hash"
);
}
$this->pbkdf2Hash = $hash;

return $this;
}

/**
* Get the Pbkdf2 hash algorithm
*
* @return string
*/
public function getPbkdf2HashAlgorithm()
{
return $this->pbkdf2Hash;
}

/**
* Encrypt then authenticate using HMAC
*
* @param string $data
* @param string $data
* @return string
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -373,7 +407,7 @@ public function encrypt($data)
$this->cipher->setSalt(Rand::getBytes($this->cipher->getSaltSize(), true));
}
// generate the encryption key and the HMAC key for the authentication
$hash = Pbkdf2::calc(self::KEY_DERIV_HMAC,
$hash = Pbkdf2::calc($this->getPbkdf2HashAlgorithm(),
$this->getKey(),
$this->getSalt(),
$this->keyIteration,
Expand All @@ -398,7 +432,7 @@ public function encrypt($data)
/**
* Decrypt
*
* @param string $data
* @param string $data
* @return string|bool
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -425,7 +459,7 @@ public function decrypt($data)
$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,
$hash = Pbkdf2::calc($this->getPbkdf2HashAlgorithm(),
$this->getKey(),
$iv,
$this->keyIteration,
Expand Down
7 changes: 7 additions & 0 deletions test/BlockCipherTest.php
Expand Up @@ -103,6 +103,13 @@ public function testSetHashAlgorithm()
$this->assertEquals('sha1', $this->blockCipher->getHashAlgorithm());
}

public function testSetPbkdf2HashAlgorithm()
{
$result = $this->blockCipher->setPbkdf2HashAlgorithm('sha1');
$this->assertEquals($result, $this->blockCipher);
$this->assertEquals('sha1', $this->blockCipher->getPbkdf2HashAlgorithm());
}

public function testSetKeyIteration()
{
$result = $this->blockCipher->setKeyIteration(1000);
Expand Down

0 comments on commit aa6bf87

Please sign in to comment.