add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / CryptLib / Key / Derivation / PBKDF / PBKDF1.php
1 <?php
2 /**
3 * An implementation of the RFC 2898 PBKDF1 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
17 namespace CryptLib\Key\Derivation\PBKDF;
18
19 use CryptLib\Hash\Hash;
20
21 /**
22 * An implementation of the RFC 2989 PBKDF1 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 */
30 class PBKDF1
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 if ($length > $size) {
48 $message = 'Length is too long for hash';
49 throw new \InvalidArgumentException($message);
50 }
51 $tmp = hash($this->hash, $password . $salt, true);
52 for ($i = 2; $i <= $iterations; $i++) {
53 $tmp = hash($this->hash, $tmp, true);
54 }
55 return substr($tmp, 0, $length);
56 }
57
58 /**
59 * Get the signature for this implementation
60 *
61 * This should include all information needed to build the same isntance
62 * later.
63 *
64 * @return string The signature for this instance
65 */
66 public function getSignature() {
67 return 'pbkdf1-' . $this->hash;
68 }
69
70 }
71