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

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
wdalmut committed Jul 26, 2012
12 parents 8a99be2 + b251bdd + 9ecdb37 + 1b357ee + 4a7c17e + a40cc7f + a000f96 + fef31dc + 2b50632 + e854c8c + 450528a + 01abc0c commit af61218
Show file tree
Hide file tree
Showing 43 changed files with 212 additions and 362 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zendframework/zend-crypt",
"description": "Zend\\Crypt component",
"description": " ",
"license": "BSD-3-Clause",
"keywords": [
"zf2",
Expand All @@ -9,11 +9,11 @@
"homepage": "https://github.com/zendframework/zend-crypt",
"autoload": {
"psr-4": {
"Zend\\Crypt\\": "src/"
"Zend\\Crypt": "src/"
}
},
"require": {
"php": ">=5.3.23",
"php": ">=5.3.3",
"zendframework/zend-math": "self.version",
"zendframework/zend-stdlib": "self.version",
"zendframework/zend-servicemanager": "self.version"
Expand All @@ -27,14 +27,14 @@
"dev-develop": "2.5-dev"
}
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master",
"phpunit/PHPUnit": "~4.0"
},
"autoload-dev": {
"psr-4": {
"ZendTest\\Crypt\\": "test/"
}
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master",
"phpunit/PHPUnit": "~4.0"
}
}
47 changes: 42 additions & 5 deletions src/BlockCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

namespace Zend\Crypt;

use Zend\Crypt\Symmetric\SymmetricInterface;
use Zend\Crypt\Hmac;
use Zend\Crypt\Utils;
use Zend\Crypt\Key\Derivation\Pbkdf2;
use Zend\Math\Math;
use Zend\Crypt\Symmetric\SymmetricInterface;
use Zend\Crypt\Utils;
use Zend\Math\Rand;

/**
* Encrypt using a symmetric cipher then authenticate using HMAC (SHA-256)
Expand Down Expand Up @@ -46,6 +46,13 @@ class BlockCipher
* @var string
*/
protected $hash = 'sha256';

/**
* Salt (IV)
*
* @var string
*/
protected $salt;

/**
* The output is binary?
Expand Down Expand Up @@ -177,6 +184,32 @@ public function getKeyIteration()
return $this->keyIteration;
}

/**
* Set the salt (IV)
*
* @param string $salt
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
public function setSalt($salt)
{
if (empty($salt)) {
throw new Exception\InvalidArgumentException("The salt (IV) cannot be empty");
}
$this->salt = $salt;
return $this;
}

/**
* Get the salt (IV)
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}

/**
* Enable/disable the binary output
*
Expand Down Expand Up @@ -318,8 +351,12 @@ public function encrypt($data)
throw new Exception\InvalidArgumentException('No symmetric cipher specified');
}
$keySize = $this->cipher->getKeySize();
// generate a random salt (IV)
$this->cipher->setSalt(Math::randBytes($this->cipher->getSaltSize(), true));
$salt = $this->getSalt();
// generate a random salt (IV) if empty
if (empty($salt)) {
$salt = Rand::getBytes($this->cipher->getSaltSize(), true);
}
$this->cipher->setSalt($salt);
// generate the encryption key and the HMAC key for the authentication
$hash = Pbkdf2::calc(self::KEY_DERIV_HMAC,
$this->getKey(),
Expand Down
2 changes: 0 additions & 2 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
/**
* @category Zend
* @package Zend_Crypt
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface ExceptionInterface
{
Expand Down
3 changes: 1 addition & 2 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Exception;

/**
Expand All @@ -15,8 +16,6 @@
* @category Zend
* @package Zend_Crypt
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidArgumentException
extends \InvalidArgumentException
Expand Down
3 changes: 1 addition & 2 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Exception;

/**
Expand All @@ -15,8 +16,6 @@
* @category Zend
* @package Zend_Crypt
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RuntimeException
extends \RuntimeException
Expand Down
5 changes: 2 additions & 3 deletions src/Key/Derivation/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Key\Derivation\Exception;

use Zend\Crypt\Exception\ExceptionInterface as Exception;

/**
* @category Zend
* @package Zend_Crypt
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface ExceptionInterface extends Exception
{}
{}
5 changes: 2 additions & 3 deletions src/Key/Derivation/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Key\Derivation\Exception;

use Zend\Crypt\Exception;
Expand All @@ -17,9 +18,7 @@
* @category Zend
* @package Zend_Crypt
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidArgumentException extends Exception\InvalidArgumentException implements
class InvalidArgumentException extends Exception\InvalidArgumentException implements
ExceptionInterface
{}
4 changes: 1 addition & 3 deletions src/Key/Derivation/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
* @category Zend
* @package Zend_Crypt
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RuntimeException extends Exception\RuntimeException implements
class RuntimeException extends Exception\RuntimeException implements
ExceptionInterface
{}
4 changes: 2 additions & 2 deletions src/Key/Derivation/Pbkdf2.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public static function calc($hash, $password, $salt, $iterations, $length)
$result = '';
for ($block = 1; $block <= $num; $block++) {
$hmac = Hmac::compute($password, $hash, $salt . pack('N', $block), Hmac::OUTPUT_BINARY);
$mix = $hmac;
$mix = $hmac;
for ($i = 1; $i < $iterations; $i++) {
$hmac = Hmac::compute($password, $hash, $hmac, Hmac::OUTPUT_BINARY);
$mix ^= $hmac;
$mix ^= $hmac;
}
$result .= $mix;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Key/Derivation/SaltedS2k.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Key\Derivation;

/**
* Salted S2K key generation (OpenPGP document, RFC 2440)
*
* @category Zend
* @package Zend_Crypt
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class SaltedS2k
{
Expand Down
22 changes: 12 additions & 10 deletions src/Password/Bcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Password;

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

/**
* Bcrypt algorithm using crypt() function of PHP
*
* @category Zend
* @package Zend_Crypt
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Bcrypt implements PasswordInterface
{
const MIN_SALT_SIZE = 16;

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

/**
* @var string
*/
Expand All @@ -47,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 @@ -73,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 @@ -86,15 +87,16 @@ public function create($password)
$prefix = '$2y$';
} else {
$prefix = '$2a$';
// check if the password contains 8-bit character
// 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/Password/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Password\Exception;

use Zend\Crypt\Exception\ExceptionInterface as Exception;

/**
* @category Zend
* @package Zend_Crypt
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface ExceptionInterface extends Exception
{}
{}
5 changes: 2 additions & 3 deletions src/Password/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Password\Exception;

use Zend\Crypt\Exception;
Expand All @@ -17,9 +18,7 @@
* @category Zend
* @package Zend_Crypt
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidArgumentException extends Exception\InvalidArgumentException implements
class InvalidArgumentException extends Exception\InvalidArgumentException implements
ExceptionInterface
{}
4 changes: 1 addition & 3 deletions src/Password/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
* @category Zend
* @package Zend_Crypt
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RuntimeException extends Exception\RuntimeException implements
class RuntimeException extends Exception\RuntimeException implements
ExceptionInterface
{}
1 change: 1 addition & 0 deletions src/Password/PasswordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Password;

interface PasswordInterface
Expand Down
Loading

0 comments on commit af61218

Please sign in to comment.