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

/application/libraries/Phpass_library.php

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