PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/class-phpass.php

https://github.com/glasklar/Owncloud-Cloudpress
PHP | 260 lines | 169 code | 39 blank | 52 comment | 43 complexity | 16377f5cd87a770b21b89abb738c2b2b MD5 | raw file
  1. <?php
  2. /**
  3. * Portable PHP password hashing framework.
  4. * @package phpass
  5. * @since 2.5
  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
  33. */
  34. class WPPasswordHash{
  35. var $itoa64;
  36. var $iteration_count_log2;
  37. var $portable_hashes;
  38. var $random_state;
  39. function WPPasswordHash($iteration_count_log2, $portable_hashes)
  40. {
  41. $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  42. if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
  43. $iteration_count_log2 = 8;
  44. $this->iteration_count_log2 = $iteration_count_log2;
  45. $this->portable_hashes = $portable_hashes;
  46. $this->random_state = microtime() . uniqid(rand(), TRUE); // removed getmypid() for compatibility reasons
  47. }
  48. function get_random_bytes($count)
  49. {
  50. $output = '';
  51. if ( @is_readable('/dev/urandom') &&
  52. ($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. $id = substr($setting, 0, 3);
  103. # We use "$P$", phpBB3 uses "$H$" for the same thing
  104. if ($id != '$P$' && $id != '$H$')
  105. return $output;
  106. $count_log2 = strpos($this->itoa64, $setting[3]);
  107. if ($count_log2 < 7 || $count_log2 > 30)
  108. return $output;
  109. $count = 1 << $count_log2;
  110. $salt = substr($setting, 4, 8);
  111. if (strlen($salt) != 8)
  112. return $output;
  113. # We're kind of forced to use MD5 here since it's the only
  114. # cryptographic primitive available in all versions of PHP
  115. # currently in use. To implement our own low-level crypto
  116. # in PHP would result in much worse performance and
  117. # consequently in lower iteration counts and hashes that are
  118. # quicker to crack (by non-PHP code).
  119. if (PHP_VERSION >= '5') {
  120. $hash = md5($salt . $password, TRUE);
  121. do {
  122. $hash = md5($hash . $password, TRUE);
  123. } while (--$count);
  124. } else {
  125. $hash = pack('H*', md5($salt . $password));
  126. do {
  127. $hash = pack('H*', md5($hash . $password));
  128. } while (--$count);
  129. }
  130. $output = substr($setting, 0, 12);
  131. $output .= $this->encode64($hash, 16);
  132. return $output;
  133. }
  134. function gensalt_extended($input)
  135. {
  136. $count_log2 = min($this->iteration_count_log2 + 8, 24);
  137. # This should be odd to not reveal weak DES keys, and the
  138. # maximum valid value is (2**24 - 1) which is odd anyway.
  139. $count = (1 << $count_log2) - 1;
  140. $output = '_';
  141. $output .= $this->itoa64[$count & 0x3f];
  142. $output .= $this->itoa64[($count >> 6) & 0x3f];
  143. $output .= $this->itoa64[($count >> 12) & 0x3f];
  144. $output .= $this->itoa64[($count >> 18) & 0x3f];
  145. $output .= $this->encode64($input, 3);
  146. return $output;
  147. }
  148. function gensalt_blowfish($input)
  149. {
  150. # This one needs to use a different order of characters and a
  151. # different encoding scheme from the one in encode64() above.
  152. # We care because the last character in our encoded string will
  153. # only represent 2 bits. While two known implementations of
  154. # bcrypt will happily accept and correct a salt string which
  155. # has the 4 unused bits set to non-zero, we do not want to take
  156. # chances and we also do not want to waste an additional byte
  157. # of entropy.
  158. $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  159. $output = '$2a$';
  160. $output .= chr(ord('0') + $this->iteration_count_log2 / 10);
  161. $output .= chr(ord('0') + $this->iteration_count_log2 % 10);
  162. $output .= '$';
  163. $i = 0;
  164. do {
  165. $c1 = ord($input[$i++]);
  166. $output .= $itoa64[$c1 >> 2];
  167. $c1 = ($c1 & 0x03) << 4;
  168. if ($i >= 16) {
  169. $output .= $itoa64[$c1];
  170. break;
  171. }
  172. $c2 = ord($input[$i++]);
  173. $c1 |= $c2 >> 4;
  174. $output .= $itoa64[$c1];
  175. $c1 = ($c2 & 0x0f) << 2;
  176. $c2 = ord($input[$i++]);
  177. $c1 |= $c2 >> 6;
  178. $output .= $itoa64[$c1];
  179. $output .= $itoa64[$c2 & 0x3f];
  180. } while (1);
  181. return $output;
  182. }
  183. function HashPassword($password)
  184. {
  185. $random = '';
  186. if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
  187. $random = $this->get_random_bytes(16);
  188. $hash =
  189. crypt($password, $this->gensalt_blowfish($random));
  190. if (strlen($hash) == 60)
  191. return $hash;
  192. }
  193. if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
  194. if (strlen($random) < 3)
  195. $random = $this->get_random_bytes(3);
  196. $hash =
  197. crypt($password, $this->gensalt_extended($random));
  198. if (strlen($hash) == 20)
  199. return $hash;
  200. }
  201. if (strlen($random) < 6)
  202. $random = $this->get_random_bytes(6);
  203. $hash =
  204. $this->crypt_private($password,
  205. $this->gensalt_private($random));
  206. if (strlen($hash) == 34)
  207. return $hash;
  208. # Returning '*' on error is safe here, but would _not_ be safe
  209. # in a crypt(3)-like function used _both_ for generating new
  210. # hashes and for validating passwords against existing hashes.
  211. return '*';
  212. }
  213. function CheckPassword($password, $stored_hash)
  214. {
  215. $hash = $this->crypt_private($password, $stored_hash);
  216. if ($hash[0] == '*')
  217. $hash = crypt($password, $stored_hash);
  218. return $hash == $stored_hash;
  219. }
  220. }
  221. ?>