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

/server/wordpress/wp-includes/class-phpass.php

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