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

Commit

Permalink
Merge remote-tracking branch 'zf2/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Jul 23, 2012
6 parents 8b31906 + 1b357ee + 4a7c17e + a40cc7f + a000f96 + fef31dc commit 01abc0c
Show file tree
Hide file tree
Showing 56 changed files with 965 additions and 1,125 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

14 changes: 0 additions & 14 deletions .travis/run-tests.sh

This file was deleted.

7 changes: 0 additions & 7 deletions .travis/skipped-components

This file was deleted.

61 changes: 0 additions & 61 deletions .travis/tested-components

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"
}
}
74 changes: 39 additions & 35 deletions src/BlockCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,60 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt;

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

/**
* Encrypt using a symmetric cipher then authenticate using HMAC (SHA-256)
*
* @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 BlockCipher
{
const KEY_DERIV_HMAC = 'sha256';

/**
* Symmetric cipher
*
* @var SymmetricInterface
*/
protected $cipher;

/**
* Symmetric cipher broker
* Symmetric cipher plugin manager
*
* @var SymmetricBroker
* @var SymmetricPluginManager
*/
protected static $symmetricBroker = null;
protected static $symmetricPlugins = null;

/**
* Hash algorithm fot HMAC
*
* @var string
*/
protected $hash = 'sha256';

/**
* The output is binary?
*
* @var boolean
*/
protected $binaryOutput = false;

/**
* User's key
*
* @var string
*/
protected $key;

/**
* Number of iterations for Pbkdf2
*
Expand All @@ -82,51 +87,50 @@ public function __construct(SymmetricInterface $cipher)
*/
public static function factory($adapter, $options = array())
{
$broker = self::getSymmetricBroker();
$adapter = $broker->load($adapter, array($options));
$plugins = self::getSymmetricPluginManager();
$adapter = $plugins->get($adapter, (array) $options);
return new self($adapter);
}

/**
* Returns the symmetric cipher broker. If it doesn't exist it's created.
* Returns the symmetric cipher plugin manager. If it doesn't exist it's created.
*
* @return SymmetricBroker
* @return SymmetricPluginManager
*/
public static function getSymmetricBroker()
public static function getSymmetricPluginManager()
{
if (self::$symmetricBroker === null) {
self::setSymmetricBroker(new SymmetricBroker());
if (self::$symmetricPlugins === null) {
self::setSymmetricPluginManager(new SymmetricPluginManager());
}

return self::$symmetricBroker;
return self::$symmetricPlugins;
}

/**
* Set the symmetric cipher broker
* Set the symmetric cipher plugin manager
*
* @param string|SymmetricBroker $broker
* @param string|SymmetricPluginManager $plugins
* @throws Exception\InvalidArgumentException
*/
public static function setSymmetricBroker($broker)
public static function setSymmetricPluginManager($plugins)
{
if (is_string($broker)) {
if (!class_exists($broker)) {
throw new Exception\InvalidArgumentException(
sprintf(
'Unable to locate symmetric cipher broker of class "%s"',
$broker
));
if (is_string($plugins)) {
if (!class_exists($plugins)) {
throw new Exception\InvalidArgumentException(sprintf(
'Unable to locate symmetric cipher plugins using class "%s"; class does not exist',
$plugins
));
}
$broker = new $broker();
$plugins = new $plugins();
}
if (!$broker instanceof SymmetricBroker) {
throw new Exception\InvalidArgumentException(
sprintf(
'Symmetric cipher broker must extend SymmetricBroker; received "%s"',
(is_object($broker) ? get_class($broker) : gettype($broker))
));
if (!$plugins instanceof SymmetricPluginManager) {
throw new Exception\InvalidArgumentException(sprintf(
'Expected an instance or extension of %s\SymmetricPluginManager; received "%s"',
__NAMESPACE__,
(is_object($plugins) ? get_class($plugins) : gettype($plugins))
));
}
self::$symmetricBroker = $broker;
self::$symmetricPlugins = $plugins;
}

/**
Expand Down Expand Up @@ -278,7 +282,7 @@ public function setHashAlgorithm($hash)
{
if (!Hash::isSupported($hash)) {
throw new Exception\InvalidArgumentException(
"The specified hash algorithm $hash is not supported by Zend\Crypt\Hash"
"The specified hash algorithm '{$hash}' is not supported by Zend\Crypt\Hash"
);
}
$this->hash = $hash;
Expand Down Expand Up @@ -328,7 +332,7 @@ public function encrypt($data)
$keyHmac = substr($hash, $keySize);
// encryption
$ciphertext = $this->cipher->encrypt($data);
// HMAC
// HMAC
$hmac = Hmac::compute($keyHmac,
$this->hash,
$this->cipher->getAlgorithm() . $ciphertext);
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
18 changes: 10 additions & 8 deletions src/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt;

/**
* @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 Hash
{
const STRING = 'string';
const BINARY = 'binary';
const OUTPUT_STRING = 'string';
const OUTPUT_BINARY = 'binary';

/**
* List of hash algorithms supported
*
Expand All @@ -33,14 +33,16 @@ class Hash
* @throws Exception\InvalidArgumentException
* @return string
*/
public static function compute($hash, $data, $output = self::STRING)
public static function compute($hash, $data, $output = self::OUTPUT_STRING)
{
$hash = strtolower($hash);
if (!self::isSupported($hash)) {
throw new Exception\InvalidArgumentException('Hash algorithm provided is not supported on this PHP installation');
throw new Exception\InvalidArgumentException(
'Hash algorithm provided is not supported on this PHP installation'
);
}

$output = ($output === self::BINARY);
$output = ($output === self::OUTPUT_BINARY);
return hash($hash, $data, $output);
}

Expand All @@ -51,7 +53,7 @@ public static function compute($hash, $data, $output = self::STRING)
* @param string $output
* @return integer
*/
public static function getOutputSize($hash, $output = self::STRING)
public static function getOutputSize($hash, $output = self::OUTPUT_STRING)
{
return strlen(self::compute($hash, 'data', $output));
}
Expand Down
Loading

0 comments on commit 01abc0c

Please sign in to comment.