PasswordHash.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. #
  3. # Portable PHP password hashing framework.
  4. #
  5. # Version 0.3 / genuine.
  6. #
  7. # Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in
  8. # the public domain. Revised in subsequent years, still public domain.
  9. #
  10. # There's absolutely no warranty.
  11. #
  12. # The homepage URL for this framework is:
  13. #
  14. # http://www.openwall.com/phpass/
  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. class PasswordHash
  28. {
  29. var $itoa64;
  30. var $iteration_count_log2;
  31. var $portable_hashes;
  32. var $random_state;
  33. function __construct($iteration_count_log2, $portable_hashes)
  34. {
  35. $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  36. if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
  37. $iteration_count_log2 = 8;
  38. $this->iteration_count_log2 = $iteration_count_log2;
  39. $this->portable_hashes = $portable_hashes;
  40. $this->random_state = microtime();
  41. if (function_exists('getmypid'))
  42. $this->random_state .= getmypid();
  43. }
  44. function get_random_bytes($count)
  45. {
  46. $output = '';
  47. if (is_readable('/dev/urandom') &&
  48. ($fh = @fopen('/dev/urandom', 'rb'))
  49. ) {
  50. $output = fread($fh, $count);
  51. fclose($fh);
  52. }
  53. if (strlen($output) < $count) {
  54. $output = '';
  55. for ($i = 0; $i < $count; $i += 16) {
  56. $this->random_state = md5(microtime() . $this->random_state);
  57. $output .=
  58. pack('H*', md5($this->random_state));
  59. }
  60. $output = substr($output, 0, $count);
  61. }
  62. return $output;
  63. }
  64. function encode64($input, $count)
  65. {
  66. $output = '';
  67. $i = 0;
  68. do {
  69. $value = ord($input[$i++]);
  70. $output .= $this->itoa64[$value & 0x3f];
  71. if ($i < $count)
  72. $value |= ord($input[$i]) << 8;
  73. $output .= $this->itoa64[($value >> 6) & 0x3f];
  74. if ($i++ >= $count)
  75. break;
  76. if ($i < $count)
  77. $value |= ord($input[$i]) << 16;
  78. $output .= $this->itoa64[($value >> 12) & 0x3f];
  79. if ($i++ >= $count)
  80. break;
  81. $output .= $this->itoa64[($value >> 18) & 0x3f];
  82. } while ($i < $count);
  83. return $output;
  84. }
  85. function gensalt_private($input)
  86. {
  87. $output = '$P$';
  88. $output .= $this->itoa64[min($this->iteration_count_log2 +
  89. ((PHP_VERSION >= '5') ? 5 : 3), 30)];
  90. $output .= $this->encode64($input, 6);
  91. return $output;
  92. }
  93. function crypt_private($password, $setting)
  94. {
  95. $output = '*0';
  96. if (substr($setting, 0, 2) == $output)
  97. $output = '*1';
  98. $id = substr($setting, 0, 3);
  99. # We use "$P$", phpBB3 uses "$H$" for the same thing
  100. if ($id != '$P$' && $id != '$H$')
  101. return $output;
  102. $count_log2 = strpos($this->itoa64, $setting[3]);
  103. if ($count_log2 < 7 || $count_log2 > 30)
  104. return $output;
  105. $count = 1 << $count_log2;
  106. $salt = substr($setting, 4, 8);
  107. if (strlen($salt) != 8)
  108. return $output;
  109. # We're kind of forced to use MD5 here since it's the only
  110. # cryptographic primitive available in all versions of PHP
  111. # currently in use. To implement our own low-level crypto
  112. # in PHP would result in much worse performance and
  113. # consequently in lower iteration counts and hashes that are
  114. # quicker to crack (by non-PHP code).
  115. if (PHP_VERSION >= '5') {
  116. $hash = md5($salt . $password, TRUE);
  117. do {
  118. $hash = md5($hash . $password, TRUE);
  119. } while (--$count);
  120. } else {
  121. $hash = pack('H*', md5($salt . $password));
  122. do {
  123. $hash = pack('H*', md5($hash . $password));
  124. } while (--$count);
  125. }
  126. $output = substr($setting, 0, 12);
  127. $output .= $this->encode64($hash, 16);
  128. return $output;
  129. }
  130. function gensalt_extended($input)
  131. {
  132. $count_log2 = min($this->iteration_count_log2 + 8, 24);
  133. # This should be odd to not reveal weak DES keys, and the
  134. # maximum valid value is (2**24 - 1) which is odd anyway.
  135. $count = (1 << $count_log2) - 1;
  136. $output = '_';
  137. $output .= $this->itoa64[$count & 0x3f];
  138. $output .= $this->itoa64[($count >> 6) & 0x3f];
  139. $output .= $this->itoa64[($count >> 12) & 0x3f];
  140. $output .= $this->itoa64[($count >> 18) & 0x3f];
  141. $output .= $this->encode64($input, 3);
  142. return $output;
  143. }
  144. function gensalt_blowfish($input)
  145. {
  146. # This one needs to use a different order of characters and a
  147. # different encoding scheme from the one in encode64() above.
  148. # We care because the last character in our encoded string will
  149. # only represent 2 bits. While two known implementations of
  150. # bcrypt will happily accept and correct a salt string which
  151. # has the 4 unused bits set to non-zero, we do not want to take
  152. # chances and we also do not want to waste an additional byte
  153. # of entropy.
  154. $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  155. $output = '$2a$';
  156. $output .= chr(ord('0') + $this->iteration_count_log2 / 10);
  157. $output .= chr(ord('0') + $this->iteration_count_log2 % 10);
  158. $output .= '$';
  159. $i = 0;
  160. do {
  161. $c1 = ord($input[$i++]);
  162. $output .= $itoa64[$c1 >> 2];
  163. $c1 = ($c1 & 0x03) << 4;
  164. if ($i >= 16) {
  165. $output .= $itoa64[$c1];
  166. break;
  167. }
  168. $c2 = ord($input[$i++]);
  169. $c1 |= $c2 >> 4;
  170. $output .= $itoa64[$c1];
  171. $c1 = ($c2 & 0x0f) << 2;
  172. $c2 = ord($input[$i++]);
  173. $c1 |= $c2 >> 6;
  174. $output .= $itoa64[$c1];
  175. $output .= $itoa64[$c2 & 0x3f];
  176. } while (1);
  177. return $output;
  178. }
  179. function HashPassword($password)
  180. {
  181. $random = '';
  182. if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
  183. $random = $this->get_random_bytes(16);
  184. $hash = crypt($password, $this->gensalt_blowfish($random));
  185. if (strlen($hash) == 60)
  186. return $hash;
  187. }
  188. if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
  189. if (strlen($random) < 3)
  190. $random = $this->get_random_bytes(3);
  191. $hash = crypt($password, $this->gensalt_extended($random));
  192. if (strlen($hash) == 20)
  193. return $hash;
  194. }
  195. if (strlen($random) < 6)
  196. $random = $this->get_random_bytes(6);
  197. $hash = $this->crypt_private($password, $this->gensalt_private($random));
  198. if (strlen($hash) == 34)
  199. return $hash;
  200. # Returning '*' on error is safe here, but would _not_ be safe
  201. # in a crypt(3)-like function used _both_ for generating new
  202. # hashes and for validating passwords against existing hashes.
  203. return '*';
  204. }
  205. function CheckPassword($password, $stored_hash)
  206. {
  207. $hash = $this->crypt_private($password, $stored_hash);
  208. if ($hash[0] == '*')
  209. $hash = crypt($password, $stored_hash);
  210. return $hash == $stored_hash;
  211. }
  212. }
  213. ?>