3 * An implementation of the crypt library's Blowfish hash method
7 * @category PHPCryptLib
9 * @subpackage Derivation
10 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
11 * @copyright 2011 The Authors
12 * @license http://www.opensource.org/licenses/mit-license.html MIT License
13 * @version Build @@version@@
16 namespace CryptLib\Key\Derivation\PBKDF
;
19 * An implementation of the crypt library's Blowfish hash method
21 * @category PHPCryptLib
23 * @subpackage Derivation
24 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
27 extends \CryptLib\Key\Derivation\AbstractDerivation
28 implements \CryptLib\Key\Derivation\PBKDF
32 * Derive a key from the supplied arguments
34 * @param string $password The password to derive from
35 * @param string $salt The salt string to use
36 * @param int $iterations The number of iterations to use
37 * @param int $length The size of the string to generate
39 * @return string The derived key
41 public function derive($password, $salt, $iterations, $length) {
42 $salt = $this->encode64($salt);
43 if (strlen($salt) > 22) {
44 $salt = substr($salt, 0, 22);
45 } elseif (strlen($salt) < 22) {
46 $salt = str_pad($salt, '0');
48 $expense = ceil(log($iterations, 2));
49 $expense = $expense < 4 ?
4 : $expense;
50 $expense = $expense > 31 ?
31 : $expense;
51 $salt = '$2a$'.str_pad($expense, 2, '0', STR_PAD_LEFT
).'$'.$salt;
52 return crypt($password, $salt);
56 * Get the signature for this implementation
58 * This should include all information needed to build the same isntance
61 * @return string The signature for this instance
63 public function getSignature() {
68 * Encode a string for a blowfish salt
70 * @param string $string The string to encode
72 * @return The encoded string
74 protected function encode64($string) {
78 base64_encode($string)