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

/library/vendors/phpass/PasswordHash.php

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