3 * An implementation of the RFC 2898 PBKDF1 Standard key derivation function
7 * @see http://www.ietf.org/rfc/rfc2898.txt
8 * @category PHPCryptLib
10 * @subpackage Derivation
11 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
12 * @copyright 2011 The Authors
13 * @license http://www.opensource.org/licenses/mit-license.html MIT License
14 * @version Build @@version@@
17 namespace CryptLib\Key\Derivation\PBKDF
;
19 use CryptLib\Hash\Hash
;
22 * An implementation of the RFC 2989 PBKDF1 Standard key derivation function
24 * @see http://www.ietf.org/rfc/rfc2898.txt
25 * @category PHPCryptLib
27 * @subpackage Derivation
28 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
31 extends \CryptLib\Key\Derivation\AbstractDerivation
32 implements \CryptLib\Key\Derivation\PBKDF
36 * Derive a key from the supplied arguments
38 * @param string $password The password to derive from
39 * @param string $salt The salt string to use
40 * @param int $iterations The number of iterations to use
41 * @param int $length The size of the string to generate
43 * @return string The derived key
45 public function derive($password, $salt, $iterations, $length) {
46 $size = Hash
::getHashSize($this->hash
);
47 if ($length > $size) {
48 $message = 'Length is too long for hash';
49 throw new \
InvalidArgumentException($message);
51 $tmp = hash($this->hash
, $password . $salt, true);
52 for ($i = 2; $i <= $iterations; $i++
) {
53 $tmp = hash($this->hash
, $tmp, true);
55 return substr($tmp, 0, $length);
59 * Get the signature for this implementation
61 * This should include all information needed to build the same isntance
64 * @return string The signature for this instance
66 public function getSignature() {
67 return 'pbkdf1-' . $this->hash
;