PasswordHash.php 6.7 KB

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