add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / CryptLib / Key / Derivation / PBKDF / PBKDF2.php
CommitLineData
14d4f286
S
1<?php
2/**
3 * An implementation of the RFC 2898 PBKDF2 Standard key derivation function
4 *
5 * PHP version 5.3
6 *
7 * @see http://www.ietf.org/rfc/rfc2898.txt
8 * @category PHPCryptLib
9 * @package Key
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@@
15 */
16
17namespace CryptLib\Key\Derivation\PBKDF;
18
19use CryptLib\Hash\Hash;
20
21/**
22 * An implementation of the RFC 2898 PBKDF2 Standard key derivation function
23 *
24 * @see http://www.ietf.org/rfc/rfc2898.txt
25 * @category PHPCryptLib
26 * @package Key
27 * @subpackage Derivation
28 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
29 */
30class PBKDF2
31 extends \CryptLib\Key\Derivation\AbstractDerivation
32 implements \CryptLib\Key\Derivation\PBKDF
33{
34
35 /**
36 * Derive a key from the supplied arguments
37 *
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
42 *
43 * @return string The derived key
44 */
45 public function derive($password, $salt, $iterations, $length) {
46 $size = Hash::getHashSize($this->hash);
47 $len = ceil($length / $size);
48 $result = '';
49 for ($i = 1; $i <= $len; $i++) {
50 $tmp = hash_hmac(
51 $this->hash,
52 $salt . pack('N', $i),
53 $password,
54 true
55 );
56 $res = $tmp;
57 for ($j = 1; $j < $iterations; $j++) {
58 $tmp = hash_hmac($this->hash, $tmp, $password, true);
59 $res ^= $tmp;
60 }
61 $result .= $res;
62 }
63 return substr($result, 0, $length);
64 }
65
66 /**
67 * Get the signature for this implementation
68 *
69 * This should include all information needed to build the same isntance
70 * later.
71 *
72 * @return string The signature for this instance
73 */
74 public function getSignature() {
75 return 'pbkdf2-' . $this->hash;
76 }
77
78}
79