Commit | Line | Data |
---|---|---|
14d4f286 S |
1 | <?php |
2 | /** | |
3 | * An implementation of the RFC 10833-2 KDF1 Standard key derivation function | |
4 | * | |
5 | * PHP version 5.3 | |
6 | * | |
7 | * @category PHPCryptLib | |
8 | * @package Key | |
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@@ | |
14 | */ | |
15 | ||
16 | namespace CryptLib\Key\Derivation\KDF; | |
17 | ||
18 | use CryptLib\Hash\Hash; | |
19 | ||
20 | /** | |
21 | * An implementation of the RFC 10833-2 KDF1 Standard key derivation function | |
22 | * | |
23 | * @category PHPCryptLib | |
24 | * @package Key | |
25 | * @subpackage Derivation | |
26 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
27 | */ | |
28 | class KDF1 | |
29 | extends \CryptLib\Key\Derivation\AbstractDerivation | |
30 | implements \CryptLib\Key\Derivation\KDF | |
31 | { | |
32 | ||
33 | /** | |
34 | * Derive a key of the specified length based on the inputted secret | |
35 | * | |
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 | |
39 | * | |
40 | * @return string The generated key | |
41 | */ | |
42 | public function derive($secret, $length, $other = '') { | |
43 | $size = Hash::getHashSize($this->hash); | |
44 | $len = ceil($length / $size); | |
45 | $res = ''; | |
46 | for ($i = 0; $i < $len; $i++) { | |
47 | $tmp = pack('N', $i); | |
48 | $res .= hash($this->hash, $secret . $tmp . $other, true); | |
49 | } | |
50 | return substr($res, 0, $length); | |
51 | } | |
52 | ||
53 | } | |
54 |