3 * An implementation of the RFC 10833-2 KDF2 Standard key derivation function
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\KDF
;
18 use CryptLib\Hash\Hash
;
21 * An implementation of the RFC 10833-2 KDF2 Standard key derivation function
23 * @category PHPCryptLib
25 * @subpackage Derivation
26 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
29 extends \CryptLib\Key\Derivation\AbstractDerivation
30 implements \CryptLib\Key\Derivation\KDF
34 * Derive a key of the specified length based on the inputted secret
36 * @param string $secret The secret to base the key on
37 * @param int $length The length of the key to derive
38 * @param string $other Additional data to append to the key
40 * @return string The generated key
42 public function derive($secret, $length, $other = '') {
43 $size = Hash
::getHashSize($this->hash
);
44 $len = ceil($length / $size);
46 for ($i = 1; $i <= $len; $i++
) {
48 $res .= hash($this->hash
, $secret . $tmp . $other, true);
50 return substr($res, 0, $length);