PageRenderTime 104ms CodeModel.GetById 41ms app.highlight 12ms RepoModel.GetById 48ms app.codeStats 0ms

/wp-includes/class-phpass.php

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