PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/importerosc/passwordhash.php

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