1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The RandomLib library for securely generating random numbers and strings in PHP |
5
|
|
|
* |
6
|
|
|
* @author Anthony Ferrara <[email protected]> |
7
|
|
|
* @copyright 2011 The Authors |
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
9
|
|
|
* @version Build @@version@@ |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The Hash medium strength mixer class |
14
|
|
|
* |
15
|
|
|
* This class implements a mixer based upon the recommendations in RFC 4086 |
16
|
|
|
* section 5.2 |
17
|
|
|
* |
18
|
|
|
* PHP version 5.3 |
19
|
|
|
* |
20
|
|
|
* @see http://tools.ietf.org/html/rfc4086#section-5.2 |
21
|
|
|
* |
22
|
|
|
* @category PHPCryptLib |
23
|
|
|
* @package Random |
24
|
|
|
* @subpackage Mixer |
25
|
|
|
* |
26
|
|
|
* @author Anthony Ferrara <[email protected]> |
27
|
|
|
* @copyright 2011 The Authors |
28
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
29
|
|
|
* |
30
|
|
|
* @version Build @@version@@ |
31
|
|
|
*/ |
32
|
|
|
namespace RandomLib\Mixer; |
33
|
|
|
|
34
|
|
|
use SecurityLib\Strength; |
35
|
|
|
use SecurityLib\Util; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The Hash medium strength mixer class |
39
|
|
|
* |
40
|
|
|
* This class implements a mixer based upon the recommendations in RFC 4086 |
41
|
|
|
* section 5.2 |
42
|
|
|
* |
43
|
|
|
* @see http://tools.ietf.org/html/rfc4086#section-5.2 |
44
|
|
|
* |
45
|
|
|
* @category PHPCryptLib |
46
|
|
|
* @package Random |
47
|
|
|
* @subpackage Mixer |
48
|
|
|
* |
49
|
|
|
* @author Anthony Ferrara <[email protected]> |
50
|
|
|
*/ |
51
|
|
|
class Hash extends \RandomLib\AbstractMixer |
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string The hash instance to use |
56
|
|
|
*/ |
57
|
|
|
protected $hash = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Build the hash mixer |
61
|
|
|
* |
62
|
|
|
* @param string $hash The hash instance to use (defaults to sha512) |
63
|
|
|
* |
64
|
|
|
* @return void |
|
|
|
|
65
|
|
|
*/ |
66
|
|
|
public function __construct($hash = 'sha512') |
67
|
|
|
{ |
68
|
|
|
$this->hash = $hash; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return an instance of Strength indicating the strength of the source |
73
|
|
|
* |
74
|
|
|
* @return \SecurityLib\Strength An instance of one of the strength classes |
75
|
|
|
*/ |
76
|
|
|
public static function getStrength() |
77
|
|
|
{ |
78
|
|
|
return new Strength(Strength::MEDIUM); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Test to see if the mixer is available |
83
|
|
|
* |
84
|
|
|
* @return bool If the mixer is available on the system |
85
|
|
|
*/ |
86
|
|
|
public static function test() |
87
|
|
|
{ |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the block size (the size of the individual blocks used for the mixing) |
93
|
|
|
* |
94
|
|
|
* @return int The block size |
95
|
|
|
*/ |
96
|
|
|
protected function getPartSize() |
97
|
|
|
{ |
98
|
|
|
return Util::safeStrlen(hash($this->hash, '', true)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Mix 2 parts together using one method |
103
|
|
|
* |
104
|
|
|
* @param string $part1 The first part to mix |
105
|
|
|
* @param string $part2 The second part to mix |
106
|
|
|
* |
107
|
|
|
* @return string The mixed data |
108
|
|
|
*/ |
109
|
|
|
protected function mixParts1($part1, $part2) |
110
|
|
|
{ |
111
|
|
|
return hash_hmac($this->hash, $part1, $part2, true); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Mix 2 parts together using another different method |
116
|
|
|
* |
117
|
|
|
* @param string $part1 The first part to mix |
118
|
|
|
* @param string $part2 The second part to mix |
119
|
|
|
* |
120
|
|
|
* @return string The mixed data |
121
|
|
|
*/ |
122
|
|
|
protected function mixParts2($part1, $part2) |
123
|
|
|
{ |
124
|
|
|
return hash_hmac($this->hash, $part2, $part1, true); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.