PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/buddypress-old/bp-forums/bbpress/bb-includes/backpress/class.passwordhash.php

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