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/50-import-functions' into develop
Browse files Browse the repository at this point in the history
Forward port #50

Conflicts:
	CHANGELOG.md
  • Loading branch information
weierophinney committed Apr 24, 2018
2 parents 40b29b5 + 9b7232f commit 79362cc
Show file tree
Hide file tree
Showing 22 changed files with 352 additions and 147 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ chronological order by release.

- Nothing.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 3.2.2 - TBD

### Added

- Nothing.

### Changed

- [#50](https://github.com/zendframework/zend-crypt/pull/50) updates all classes to import functions and constants they use.

### Deprecated

- Nothing.
Expand Down
31 changes: 21 additions & 10 deletions src/BlockCipher.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-crypt for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-crypt/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Crypt;
Expand All @@ -15,6 +13,19 @@
use Zend\Crypt\Symmetric\SymmetricInterface;
use Zend\Math\Rand;

use function base64_decode;
use function base64_encode;
use function class_exists;
use function gettype;
use function get_class;
use function in_array;
use function is_array;
use function is_object;
use function is_string;
use function is_subclass_of;
use function mb_substr;
use function sprintf;

/**
* Encrypt using a symmetric cipher then authenticate using HMAC (SHA-256)
*/
Expand Down Expand Up @@ -131,7 +142,7 @@ public static function getSymmetricPluginManager()
public static function setSymmetricPluginManager($plugins)
{
if (is_string($plugins)) {
if (!class_exists($plugins) || ! is_subclass_of($plugins, ContainerInterface::class)) {
if (! class_exists($plugins) || ! is_subclass_of($plugins, ContainerInterface::class)) {
throw new Exception\InvalidArgumentException(sprintf(
'Unable to locate symmetric cipher plugins using class "%s"; '
. 'class does not exist or does not implement ContainerInterface',
Expand All @@ -140,10 +151,10 @@ public static function setSymmetricPluginManager($plugins)
}
$plugins = new $plugins();
}
if (!$plugins instanceof ContainerInterface) {
if (! $plugins instanceof ContainerInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'Symmetric plugin must implements Interop\Container\ContainerInterface;; received "%s"',
(is_object($plugins) ? get_class($plugins) : gettype($plugins))
is_object($plugins) ? get_class($plugins) : gettype($plugins)
));
}
static::$symmetricPlugins = $plugins;
Expand Down Expand Up @@ -398,7 +409,7 @@ public function encrypt($data)
}

// Cast to string prior to encrypting
if (!is_string($data)) {
if (! is_string($data)) {
$data = (string) $data;
}

Expand Down Expand Up @@ -444,7 +455,7 @@ public function encrypt($data)
*/
public function decrypt($data)
{
if (!is_string($data)) {
if (! is_string($data)) {
throw new Exception\InvalidArgumentException('The data to decrypt must be a string');
}
if ('' === $data) {
Expand Down
26 changes: 18 additions & 8 deletions src/FileCipher.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-crypt for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-crypt/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Crypt;

use Zend\Crypt\Key\Derivation\Pbkdf2;
use Zend\Math\Rand;

use function fclose;
use function filesize;
use function file_exists;
use function fopen;
use function fread;
use function fseek;
use function fwrite;
use function mb_strlen;
use function mb_substr;
use function sprintf;
use function str_repeat;

/**
* Encrypt/decrypt a file using a symmetric cipher in CBC mode
* then authenticate using HMAC
Expand Down Expand Up @@ -170,7 +180,7 @@ public function getCipherSupportedAlgorithms()
*/
public function setHashAlgorithm($hash)
{
if (!Hash::isSupported($hash)) {
if (! Hash::isSupported($hash)) {
throw new Exception\InvalidArgumentException(
"The specified hash algorithm '{$hash}' is not supported by Zend\Crypt\Hash"
);
Expand All @@ -196,7 +206,7 @@ public function getHashAlgorithm()
*/
public function setPbkdf2HashAlgorithm($hash)
{
if (!Hash::isSupported($hash)) {
if (! Hash::isSupported($hash)) {
throw new Exception\InvalidArgumentException(
"The specified hash algorithm '{$hash}' is not supported by Zend\Crypt\Hash"
);
Expand Down Expand Up @@ -369,7 +379,7 @@ public function decrypt($fileIn, $fileOut)
*/
protected function checkFileInOut($fileIn, $fileOut)
{
if (!file_exists($fileIn)) {
if (! file_exists($fileIn)) {
throw new Exception\InvalidArgumentException(sprintf(
'I cannot open the %s file',
$fileIn
Expand Down
16 changes: 10 additions & 6 deletions src/Hash.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-crypt for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-crypt/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Crypt;

use function hash;
use function hash_algos;
use function in_array;
use function mb_strlen;
use function strtolower;

class Hash
{
const OUTPUT_STRING = false;
Expand All @@ -30,7 +34,7 @@ class Hash
*/
public static function compute($hash, $data, $output = self::OUTPUT_STRING)
{
if (!$hash || ($hash !== static::$lastAlgorithmSupported && !static::isSupported($hash))) {
if (! $hash || ($hash !== static::$lastAlgorithmSupported && ! static::isSupported($hash))) {
throw new Exception\InvalidArgumentException(
'Hash algorithm provided is not supported on this PHP installation'
);
Expand Down
16 changes: 10 additions & 6 deletions src/Hmac.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-crypt for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-crypt/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Crypt;

use function hash_algos;
use function hash_hmac;
use function in_array;
use function mb_strlen;
use function strtolower;

/**
* PHP implementation of the RFC 2104 Hash based Message Authentication Code
*/
Expand Down Expand Up @@ -42,7 +46,7 @@ public static function compute($key, $hash, $data, $output = self::OUTPUT_STRING
throw new Exception\InvalidArgumentException('Provided key is null or empty');
}

if (!$hash || ($hash !== static::$lastAlgorithmSupported && !static::isSupported($hash))) {
if (! $hash || ($hash !== static::$lastAlgorithmSupported && ! static::isSupported($hash))) {
throw new Exception\InvalidArgumentException(
"Hash algorithm is not supported on this PHP installation; provided '{$hash}'"
);
Expand Down
18 changes: 12 additions & 6 deletions src/Hybrid.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-crypt for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-crypt/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Crypt;
Expand All @@ -13,6 +11,14 @@
use Zend\Crypt\PublicKey\Rsa\PublicKey as PubKey;
use Zend\Crypt\PublicKey\Rsa\PrivateKey;

use function array_search;
use function base64_decode;
use function base64_encode;
use function explode;
use function is_array;
use function is_string;
use function sprintf;

/**
* Hybrid encryption (OpenPGP like)
*
Expand Down Expand Up @@ -109,7 +115,7 @@ public function decrypt($msg, $privateKey = null, $passPhrase = null, $id = null
}

if (! $privateKey instanceof PrivateKey && ! is_string($privateKey)) {
throw new Exception\RuntimeException(sprintf(
throw new Exception\RuntimeException(\sprintf(
"The private key must be a string in PEM format or an instance of %s",
PrivateKey::class
));
Expand Down
15 changes: 9 additions & 6 deletions src/Key/Derivation/Pbkdf2.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-crypt for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-crypt/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Crypt\Key\Derivation;

use Zend\Crypt\Hmac;

use function ceil;
use function hash_hmac;
use function mb_substr;
use function pack;

/**
* PKCS #5 v2.0 standard RFC 2898
*/
Expand All @@ -29,7 +32,7 @@ class Pbkdf2
*/
public static function calc($hash, $password, $salt, $iterations, $length)
{
if (!Hmac::isSupported($hash)) {
if (! Hmac::isSupported($hash)) {
throw new Exception\InvalidArgumentException("The hash algorithm $hash is not supported by " . __CLASS__);
}

Expand Down
15 changes: 9 additions & 6 deletions src/Key/Derivation/SaltedS2k.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-crypt for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-crypt/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Crypt\Key\Derivation;

use function array_keys;
use function in_array;
use function mb_strlen;
use function mhash_keygen_s2k;

/**
* Salted S2K key generation (OpenPGP document, RFC 2440)
*/
Expand Down Expand Up @@ -61,7 +64,7 @@ class SaltedS2k
*/
public static function calc($hash, $password, $salt, $bytes)
{
if (!in_array($hash, array_keys(static::$supportedMhashAlgos))) {
if (! in_array($hash, array_keys(static::$supportedMhashAlgos))) {
throw new Exception\InvalidArgumentException("The hash algorithm $hash is not supported by " . __CLASS__);
}
if (mb_strlen($salt, '8bit') < 8) {
Expand Down
18 changes: 13 additions & 5 deletions src/Key/Derivation/Scrypt.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-crypt for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-crypt/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Crypt\Key\Derivation;

use const PHP_INT_MAX;
use const PHP_INT_SIZE;

use function extension_loaded;
use function hex2bin;
use function mb_substr;
use function pack;
use function scrypt;
use function unpack;

/**
* Scrypt key derivation function
*
Expand Down

0 comments on commit 79362cc

Please sign in to comment.