This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zendframework/zendframework#1 from zendframework/d…
…evelop Develop
- Loading branch information
89 parents
782c5c5
+
0d5bda3
+
cf06367
+
9e95067
+
b9e33a1
+
6c7e92b
+
d891fb7
+
7b113a6
+
15ac5dd
+
05957dd
+
9432969
+
69b18f8
+
6c20354
+
b2e0c83
+
6228d00
+
1a4abea
+
e547383
+
1a897b1
+
77e02de
+
c70d6ac
+
b1ba719
+
c9b431d
+
0639e78
+
dacba6f
+
4060bf3
+
c24286b
+
68b9818
+
61ca1b9
+
cbe8a27
+
0bf0647
+
6893542
+
b251bdd
+
9ecdb37
+
1b357ee
+
4a7c17e
+
a40cc7f
+
a000f96
+
fef31dc
+
2b50632
+
e854c8c
+
450528a
+
01abc0c
+
af61218
+
9cff8b8
+
a212dd6
+
ce1ba30
+
0394674
+
4c09673
+
448a566
+
90e44d3
+
b9a7700
+
a4681fc
+
3419ed9
+
3771323
+
ef6db4b
+
204cdac
+
70d5d09
+
86ed85d
+
f7bd947
+
a1f37b9
+
e5f62f2
+
2700205
+
3149eaf
+
df4d984
+
bc54625
+
eac67c3
+
f021fa1
+
f1a161a
+
29824cb
+
b25223b
+
3ebfe30
+
4f5c924
+
e01927a
+
e3fb995
+
414cbca
+
f63c096
+
7d3ed79
+
ff2b316
+
793a217
+
6016f73
+
f40e1d4
+
caff185
+
24f0ea7
+
49fc55c
+
7e1ad2c
+
348cdb0
+
cf85271
+
9d21ca6
+
a166c04
commit 336789f
Showing
4 changed files
with
179 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Crypt\Password; | ||
|
||
use Zend\Crypt\Hash; | ||
|
||
/** | ||
* Bcrypt algorithm using crypt() function of PHP with password | ||
* hashed using SHA2 to allow for passwords >72 characters. | ||
*/ | ||
class BcryptSha extends Bcrypt | ||
{ | ||
|
||
/** | ||
* BcryptSha | ||
* | ||
* @param string $password | ||
* @throws Exception\RuntimeException | ||
* @return string | ||
*/ | ||
public function create($password) | ||
{ | ||
return parent::create(Hash::compute('sha256', $password)); | ||
} | ||
|
||
/** | ||
* Verify if a password is correct against a hash value | ||
* | ||
* @param string $password | ||
* @param string $hash | ||
* @throws Exception\RuntimeException when the hash is unable to be processed | ||
* @return bool | ||
*/ | ||
public function verify($password, $hash) | ||
{ | ||
return parent::verify(Hash::compute('sha256', $password), $hash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Crypt\Password; | ||
|
||
use Zend\Crypt\Password\Bcrypt; | ||
use Zend\Crypt\Password\BcryptSha; | ||
use Zend\Config\Config; | ||
use Zend\Crypt\Password\Exception; | ||
|
||
/** | ||
* @group Zend_Crypt | ||
*/ | ||
class BcryptShaTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var Bcrypt */ | ||
private $bcrypt; | ||
/** @var string */ | ||
private $salt; | ||
/** @var string */ | ||
private $bcryptPassword; | ||
/** @var string */ | ||
private $password; | ||
|
||
public function setUp() | ||
{ | ||
$this->bcrypt = new BcryptSha(); | ||
$this->salt = '1234567890123456'; | ||
$this->password = 'test'; | ||
$this->prefix = '$2y$'; | ||
|
||
$this->bcryptPassword = $this->prefix . '10$MTIzNDU2Nzg5MDEyMzQ1NeqZGfIabq2.v6vX10KI4/z0pMoIoDyVa'; | ||
} | ||
|
||
public function testConstructByOptions() | ||
{ | ||
$options = array( | ||
'cost' => '15', | ||
'salt' => $this->salt | ||
); | ||
$bcrypt = new BcryptSha($options); | ||
$this->assertTrue($bcrypt instanceof BcryptSha); | ||
$this->assertEquals('15', $bcrypt->getCost()); | ||
$this->assertEquals($this->salt, $bcrypt->getSalt()); | ||
} | ||
|
||
public function testConstructByConfig() | ||
{ | ||
$options = array( | ||
'cost' => '15', | ||
'salt' => $this->salt | ||
); | ||
$config = new Config($options); | ||
$bcrypt = new BcryptSha($config); | ||
$this->assertTrue($bcrypt instanceof BcryptSha); | ||
$this->assertEquals('15', $bcrypt->getCost()); | ||
$this->assertEquals($this->salt, $bcrypt->getSalt()); | ||
} | ||
|
||
public function testWrongConstruct() | ||
{ | ||
$this->setExpectedException('Zend\Crypt\Password\Exception\InvalidArgumentException', | ||
'The options parameter must be an array or a Traversable'); | ||
$bcrypt = new BcryptSha('test'); | ||
} | ||
|
||
public function testSetCost() | ||
{ | ||
$this->bcrypt->setCost('16'); | ||
$this->assertEquals('16', $this->bcrypt->getCost()); | ||
} | ||
|
||
public function testSetWrongCost() | ||
{ | ||
$this->setExpectedException('Zend\Crypt\Password\Exception\InvalidArgumentException', | ||
'The cost parameter of bcrypt must be in range 04-31'); | ||
$this->bcrypt->setCost('3'); | ||
} | ||
|
||
public function testSetSalt() | ||
{ | ||
$this->bcrypt->setSalt($this->salt); | ||
$this->assertEquals($this->salt, $this->bcrypt->getSalt()); | ||
} | ||
|
||
public function testSetSmallSalt() | ||
{ | ||
$this->setExpectedException('Zend\Crypt\Password\Exception\InvalidArgumentException', | ||
'The length of the salt must be at least ' . Bcrypt::MIN_SALT_SIZE . ' bytes'); | ||
$this->bcrypt->setSalt('small salt'); | ||
} | ||
|
||
public function testCreateWithRandomSalt() | ||
{ | ||
$password = $this->bcrypt->create('test'); | ||
$this->assertTrue(!empty($password)); | ||
$this->assertTrue(strlen($password) === 60); | ||
} | ||
|
||
public function testCreateWithSalt() | ||
{ | ||
$this->bcrypt->setSalt($this->salt); | ||
$password = $this->bcrypt->create($this->password); | ||
$this->assertEquals($password, $this->bcryptPassword); | ||
} | ||
|
||
public function testVerify() | ||
{ | ||
$this->assertTrue($this->bcrypt->verify($this->password, $this->bcryptPassword)); | ||
$this->assertFalse($this->bcrypt->verify(substr($this->password, -1), $this->bcryptPassword)); | ||
} | ||
|
||
public function testPasswordWith8bitCharacter() | ||
{ | ||
$password = 'test' . chr(128); | ||
$this->bcrypt->setSalt($this->salt); | ||
|
||
$this->assertEquals('$2y$10$MTIzNDU2Nzg5MDEyMzQ1NetiAf47gp.MSGw.8x1/hESvXYfMep1em', | ||
$this->bcrypt->create($password)); | ||
} | ||
} |