1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CloudControl\Cms\crypt { |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Crypt |
7
|
|
|
* @package CloudControl\Cms\crypt |
8
|
|
|
*/ |
9
|
|
|
class Crypt |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $lastSalt; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Encrypts the given value using the blowfish algorithm |
18
|
|
|
* |
19
|
|
|
* @param string $value The sting to be encrypted |
20
|
|
|
* @param int $encryptionIterations The amount of iterations used for encryption, 13 by default, resulting in aprox. 0.5 seconds of encrypting. Each raise, will result in about double the time |
21
|
|
|
* @return string The hash |
22
|
|
|
*/ |
23
|
|
|
public function encrypt($value, $encryptionIterations = 13) |
24
|
|
|
{ |
25
|
|
|
$random = $this->getRandomBytes(16); |
26
|
|
|
$this->lastSalt = $this->getSalt($random, $encryptionIterations); |
27
|
|
|
$hash = crypt($value, $this->lastSalt); |
28
|
|
|
return $hash; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* If on Linux, tries to use built in random byte feed |
33
|
|
|
* else generates its own feed |
34
|
|
|
* |
35
|
|
|
* @param int $count The amount of bytes to generates |
36
|
|
|
* @return string The bytes |
37
|
|
|
*/ |
38
|
|
|
private function getRandomBytes($count) |
39
|
|
|
{ |
40
|
|
|
$output = ''; |
41
|
|
|
$random_state = microtime(); |
42
|
|
|
|
43
|
|
|
$openBasedir = ini_get('open_basedir'); |
44
|
|
|
if (empty($openBasedir) && |
45
|
|
|
is_readable('/dev/urandom') && |
46
|
|
|
($fh = @fopen('/dev/urandom', 'rb'))) { |
47
|
|
|
$output = fread($fh, $count); |
48
|
|
|
fclose($fh); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (strlen($output) < $count) { |
52
|
|
|
$output = ''; |
53
|
|
|
for ($i = 0; $i < $count; $i += 16) { |
54
|
|
|
$random_state = |
55
|
|
|
md5(microtime() . $random_state); |
56
|
|
|
$output .= |
57
|
|
|
pack('H*', md5($random_state)); |
58
|
|
|
} |
59
|
|
|
$output = substr($output, 0, $count); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $output; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Generates the salt used for encryption |
67
|
|
|
* |
68
|
|
|
* @param string $input Feed for iteration |
69
|
|
|
* @param int $iterations Amount of iterations |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
private function getSalt($input, $iterations) |
74
|
|
|
{ |
75
|
|
|
$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
76
|
|
|
|
77
|
|
|
$output = '$2a$'; |
78
|
|
|
$output .= chr(ord('0') + $iterations / 10); |
79
|
|
|
$output .= chr(ord('0') + $iterations % 10); |
80
|
|
|
$output .= '$'; |
81
|
|
|
|
82
|
|
|
$i = 0; |
83
|
|
|
do { |
84
|
|
|
$c1 = ord($input[$i++]); |
85
|
|
|
$output .= $itoa64[$c1 >> 2]; |
86
|
|
|
$c1 = ($c1 & 0x03) << 4; |
87
|
|
|
if ($i >= 16) { |
88
|
|
|
$output .= $itoa64[$c1]; |
89
|
|
|
break; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$c2 = ord($input[$i++]); |
93
|
|
|
$c1 |= $c2 >> 4; |
94
|
|
|
$output .= $itoa64[$c1]; |
95
|
|
|
$c1 = ($c2 & 0x0f) << 2; |
96
|
|
|
|
97
|
|
|
$c2 = ord($input[$i++]); |
98
|
|
|
$c1 |= $c2 >> 6; |
99
|
|
|
$output .= $itoa64[$c1]; |
100
|
|
|
$output .= $itoa64[$c2 & 0x3f]; |
101
|
|
|
} while (1); |
102
|
|
|
|
103
|
|
|
return $output; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the last used salt for encryption |
108
|
|
|
* |
109
|
|
|
* @return string | NULL |
110
|
|
|
*/ |
111
|
|
|
public function getLastSalt() |
112
|
|
|
{ |
113
|
|
|
return $this->lastSalt; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Compare the input with a known hash and salt |
118
|
|
|
* |
119
|
|
|
* @param $input |
120
|
|
|
* @param $hash |
121
|
|
|
* @param $salt |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function compare($input, $hash, $salt) |
125
|
|
|
{ |
126
|
|
|
$newHash = crypt($input, $salt); |
127
|
|
|
return $newHash == $hash; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |