Completed
Push — master ( 20d8fe...40dd32 )
by Harald
09:20 queued 04:54
created

PasswordHash::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
#
3
# Portable PHP password hashing framework.
4
#
5
# Version 0.3 / genuine.
6
# Version 0.3 / osCommerce:
7
#   * Silenced @is_readable('/dev/urandom'))
8
#   * Added stream_set_read_buffer() when reading from /dev/urandom
9
#   * Added openssl_random_pseudo_bytes() and mcrypt_create_iv() to
10
#     get_random_bytes()
11
#
12
# Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in
13
# the public domain.  Revised in subsequent years, still public domain.
14
#
15
# There's absolutely no warranty.
16
#
17
# The homepage URL for this framework is:
18
#
19
#	http://www.openwall.com/phpass/
20
#
21
# Please be sure to update the Version line if you edit this file in any way.
22
# It is suggested that you leave the main version number intact, but indicate
23
# your project name (after the slash) and add your own revision information.
24
#
25
# Please do not change the "private" password hashing method implemented in
26
# here, thereby making your hashes incompatible.  However, if you must, please
27
# change the hash type identifier (the "$P$") to something different.
28
#
29
# Obviously, since this code is in the public domain, the above are not
30
# requirements (there can be none), but merely suggestions.
31
#
32
class PasswordHash {
33
	var $itoa64;
34
	var $iteration_count_log2;
35
	var $portable_hashes;
36
	var $random_state;
37
38
	function __construct($iteration_count_log2, $portable_hashes)
39
	{
40
		$this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
41
42
		if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
43
			$iteration_count_log2 = 8;
44
		$this->iteration_count_log2 = $iteration_count_log2;
45
46
		$this->portable_hashes = $portable_hashes;
47
48
		$this->random_state = microtime();
49
		if (function_exists('getmypid'))
50
			$this->random_state .= getmypid();
51
	}
52
53
	function get_random_bytes($count)
54
	{
55
		$output = '';
56
		if (@is_readable('/dev/urandom') &&
57
		    ($fh = @fopen('/dev/urandom', 'rb'))) {
58
			if (function_exists('stream_set_read_buffer')) {
59
				stream_set_read_buffer($fh, 0);
60
			}
61
			$output = fread($fh, $count);
62
			fclose($fh);
63
		} elseif ( function_exists('openssl_random_pseudo_bytes') ) {
64
			$output = openssl_random_pseudo_bytes($count, $orpb_secure);
65
66
			if ( $orpb_secure != true ) {
67
				$output = '';
68
			}
69
		} elseif (defined('MCRYPT_DEV_URANDOM')) {
70
			$output = mcrypt_create_iv($count, MCRYPT_DEV_URANDOM);
71
		}
72
73
		if (strlen($output) < $count) {
74
			$output = '';
75
			for ($i = 0; $i < $count; $i += 16) {
76
				$this->random_state =
77
				    md5(microtime() . $this->random_state);
78
				$output .=
79
				    pack('H*', md5($this->random_state));
80
			}
81
			$output = substr($output, 0, $count);
82
		}
83
84
		return $output;
85
	}
86
87
	function encode64($input, $count)
88
	{
89
		$output = '';
90
		$i = 0;
91
		do {
92
			$value = ord($input[$i++]);
93
			$output .= $this->itoa64[$value & 0x3f];
94
			if ($i < $count)
95
				$value |= ord($input[$i]) << 8;
96
			$output .= $this->itoa64[($value >> 6) & 0x3f];
97
			if ($i++ >= $count)
98
				break;
99
			if ($i < $count)
100
				$value |= ord($input[$i]) << 16;
101
			$output .= $this->itoa64[($value >> 12) & 0x3f];
102
			if ($i++ >= $count)
103
				break;
104
			$output .= $this->itoa64[($value >> 18) & 0x3f];
105
		} while ($i < $count);
106
107
		return $output;
108
	}
109
110
	function gensalt_private($input)
111
	{
112
		$output = '$P$';
113
		$output .= $this->itoa64[min($this->iteration_count_log2 +
114
			((PHP_VERSION >= '5') ? 5 : 3), 30)];
115
		$output .= $this->encode64($input, 6);
116
117
		return $output;
118
	}
119
120
	function crypt_private($password, $setting)
121
	{
122
		$output = '*0';
123
		if (substr($setting, 0, 2) == $output)
124
			$output = '*1';
125
126
		$id = substr($setting, 0, 3);
127
		# We use "$P$", phpBB3 uses "$H$" for the same thing
128
		if ($id != '$P$' && $id != '$H$')
129
			return $output;
130
131
		$count_log2 = strpos($this->itoa64, $setting[3]);
132
		if ($count_log2 < 7 || $count_log2 > 30)
133
			return $output;
134
135
		$count = 1 << $count_log2;
136
137
		$salt = substr($setting, 4, 8);
138
		if (strlen($salt) != 8)
139
			return $output;
140
141
		# We're kind of forced to use MD5 here since it's the only
142
		# cryptographic primitive available in all versions of PHP
143
		# currently in use.  To implement our own low-level crypto
144
		# in PHP would result in much worse performance and
145
		# consequently in lower iteration counts and hashes that are
146
		# quicker to crack (by non-PHP code).
147
		$hash = md5($salt . $password, TRUE);
148
		do {
149
			$hash = md5($hash . $password, TRUE);
150
		} while (--$count);
151
152
		$output = substr($setting, 0, 12);
153
		$output .= $this->encode64($hash, 16);
154
155
		return $output;
156
	}
157
158
	function gensalt_extended($input)
159
	{
160
		$count_log2 = min($this->iteration_count_log2 + 8, 24);
161
		# This should be odd to not reveal weak DES keys, and the
162
		# maximum valid value is (2**24 - 1) which is odd anyway.
163
		$count = (1 << $count_log2) - 1;
164
165
		$output = '_';
166
		$output .= $this->itoa64[$count & 0x3f];
167
		$output .= $this->itoa64[($count >> 6) & 0x3f];
168
		$output .= $this->itoa64[($count >> 12) & 0x3f];
169
		$output .= $this->itoa64[($count >> 18) & 0x3f];
170
171
		$output .= $this->encode64($input, 3);
172
173
		return $output;
174
	}
175
176
	function gensalt_blowfish($input)
177
	{
178
		# This one needs to use a different order of characters and a
179
		# different encoding scheme from the one in encode64() above.
180
		# We care because the last character in our encoded string will
181
		# only represent 2 bits.  While two known implementations of
182
		# bcrypt will happily accept and correct a salt string which
183
		# has the 4 unused bits set to non-zero, we do not want to take
184
		# chances and we also do not want to waste an additional byte
185
		# of entropy.
186
		$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
187
188
		$output = '$2a$';
189
		$output .= chr(ord('0') + $this->iteration_count_log2 / 10);
190
		$output .= chr(ord('0') + $this->iteration_count_log2 % 10);
191
		$output .= '$';
192
193
		$i = 0;
194
		do {
195
			$c1 = ord($input[$i++]);
196
			$output .= $itoa64[$c1 >> 2];
197
			$c1 = ($c1 & 0x03) << 4;
198
			if ($i >= 16) {
199
				$output .= $itoa64[$c1];
200
				break;
201
			}
202
203
			$c2 = ord($input[$i++]);
204
			$c1 |= $c2 >> 4;
205
			$output .= $itoa64[$c1];
206
			$c1 = ($c2 & 0x0f) << 2;
207
208
			$c2 = ord($input[$i++]);
209
			$c1 |= $c2 >> 6;
210
			$output .= $itoa64[$c1];
211
			$output .= $itoa64[$c2 & 0x3f];
212
		} while (1);
213
214
		return $output;
215
	}
216
217
	function HashPassword($password)
218
	{
219
		$random = '';
220
221
		if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
222
			$random = $this->get_random_bytes(16);
223
			$hash =
224
			    crypt($password, $this->gensalt_blowfish($random));
225
			if (strlen($hash) == 60)
226
				return $hash;
227
		}
228
229
		if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
230
			if (strlen($random) < 3)
231
				$random = $this->get_random_bytes(3);
232
			$hash =
233
			    crypt($password, $this->gensalt_extended($random));
234
			if (strlen($hash) == 20)
235
				return $hash;
236
		}
237
238
		if (strlen($random) < 6)
239
			$random = $this->get_random_bytes(6);
240
		$hash =
241
		    $this->crypt_private($password,
242
		    $this->gensalt_private($random));
243
		if (strlen($hash) == 34)
244
			return $hash;
245
246
		# Returning '*' on error is safe here, but would _not_ be safe
247
		# in a crypt(3)-like function used _both_ for generating new
248
		# hashes and for validating passwords against existing hashes.
249
		return '*';
250
	}
251
252
	function CheckPassword($password, $stored_hash)
253
	{
254
		$hash = $this->crypt_private($password, $stored_hash);
255
		if ($hash[0] == '*')
256
			$hash = crypt($password, $stored_hash);
257
258
		return $hash == $stored_hash;
259
	}
260
}
261
262
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
263