PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-phpass.php

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