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

Commit

Permalink
Merge branch 'feature/rand-refactor' of https://github.com/denixport/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jul 24, 2012
11 parents 1b357ee + 4a7c17e + a40cc7f + a000f96 + fef31dc + 2b50632 + e854c8c + 450528a + 01abc0c + a277ee3 + 522f3e5 commit 9ecdb37
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/BlockCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Zend\Crypt\Key\Derivation\Pbkdf2;
use Zend\Crypt\Symmetric\SymmetricInterface;
use Zend\Crypt\Utils;
use Zend\Math\Math;
use Zend\Math\Rand;

/**
* Encrypt using a symmetric cipher then authenticate using HMAC (SHA-256)
Expand Down Expand Up @@ -319,7 +319,7 @@ public function encrypt($data)
}
$keySize = $this->cipher->getKeySize();
// generate a random salt (IV)
$this->cipher->setSalt(Math::randBytes($this->cipher->getSaltSize(), true));
$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,
$this->getKey(),
Expand Down
15 changes: 9 additions & 6 deletions src/Password/Bcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use Traversable;
use Zend\Math\Exception as MathException;
use Zend\Math\Math;
use Zend\Math\Rand;
use Zend\Stdlib\ArrayUtils;

/**
Expand All @@ -24,10 +24,12 @@
class Bcrypt implements PasswordInterface
{
const MIN_SALT_SIZE = 16;

/**
* @var string
*/
protected $cost = '14';

/**
* @var string
*/
Expand All @@ -46,7 +48,7 @@ public function __construct($options = array())
$options = ArrayUtils::iteratorToArray($options);
} elseif (!is_array($options)) {
throw new Exception\InvalidArgumentException(
'The options parameter must be an array, a Zend\Config\Config object or a Traversable'
'The options parameter must be an array or a Traversable'
);
}
foreach ($options as $key => $value) {
Expand All @@ -72,7 +74,7 @@ public function __construct($options = array())
public function create($password)
{
if (empty($this->salt)) {
$salt = Math::randBytes(self::MIN_SALT_SIZE);
$salt = Rand::getBytes(self::MIN_SALT_SIZE);
} else {
$salt = $this->salt;
}
Expand All @@ -88,12 +90,13 @@ public function create($password)
// check if the password contains 8-bit character
if (preg_match('/[\x80-\xFF]/', $password)) {
throw new Exception\RuntimeException(
'The bcrypt implementation used by PHP can contains a security flaw using password with 8-bit character. ' .
'We suggest to upgrade to PHP 5.3.7+ or use passwords with only 7-bit characters'
'The bcrypt implementation used by PHP can contains a security flaw ' .
'using password with 8-bit character. ' .
'We suggest to upgrade to PHP 5.3.7+ or use passwords with only 7-bit characters'
);
}
}
$hash = crypt($password, $prefix . $this->cost . '$' . $salt64);
$hash = crypt($password, $prefix . $this->cost . '$' . $salt64);
if (strlen($hash) <= 13) {
throw new Exception\RuntimeException('Error during the bcrypt generation');
}
Expand Down
5 changes: 2 additions & 3 deletions src/PublicKey/DiffieHellman.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class DiffieHellman
private $privateKey = null;

/**
* BigInteger support object courtesy of Zend\Math\Math
* BigInteger support object courtesy of Zend\Math
*
* @var \Zend\Math\BigInteger\Adapter\AdapterInterface
*/
Expand Down Expand Up @@ -438,7 +438,6 @@ protected function convert($number, $inputFormat = self::FORMAT_NUMBER,
*/
protected function generatePrivateKey()
{
$rand = Math\Math::randBytes(strlen($this->getPrime()), true);
return $rand;
return Math\Rand::getBytes(strlen($this->getPrime()), true);
}
}
8 changes: 5 additions & 3 deletions test/Password/BcryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testConstructByConfig()
public function testWrongConstruct()
{
$this->setExpectedException('Zend\Crypt\Password\Exception\InvalidArgumentException',
'The options parameter must be an array, a Zend\Config\Config object or a Traversable');
'The options parameter must be an array or a Traversable');
$bcrypt = new Bcrypt('test');
}

Expand Down Expand Up @@ -128,10 +128,12 @@ public function testPasswordWith8bitCharacter()
$this->bcrypt->setSalt($this->salt);

if (version_compare(PHP_VERSION, '5.3.7') >= 0) {
$this->assertEquals('$2y$14$MTIzNDU2Nzg5MDEyMzQ1NexAbOIUHkG6Ra.TK9QxHOVUhDxOe4dkW', $this->bcrypt->create($password));
$this->assertEquals('$2y$14$MTIzNDU2Nzg5MDEyMzQ1NexAbOIUHkG6Ra.TK9QxHOVUhDxOe4dkW',
$this->bcrypt->create($password));
} else {
$this->setExpectedException('Zend\Crypt\Password\Exception\RuntimeException',
'The bcrypt implementation used by PHP can contains a security flaw using password with 8-bit character. ' .
'The bcrypt implementation used by PHP can contains a security flaw ' .
'using password with 8-bit character. ' .
'We suggest to upgrade to PHP 5.3.7+ or use passwords with only 7-bit characters'
);
$output = $this->bcrypt->create($password);
Expand Down

0 comments on commit 9ecdb37

Please sign in to comment.