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

/vendor/ircmaxell/password-compat/lib/password.php

https://gitlab.com/techniconline/kmc
PHP | 321 lines | 209 code | 22 blank | 90 comment | 60 complexity | ff51226de4d0e3db8359a7051c75214f MD5 | raw file
  1. <?php
  2. /**
  3. * A Compatibility library with PHP 5.5's simplified password hashing API.
  4. *
  5. * @author Anthony Ferrara <ircmaxell@php.net>
  6. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  7. * @copyright 2012 The Authors
  8. */
  9. namespace {
  10. if (!defined('PASSWORD_BCRYPT')) {
  11. /**
  12. * PHPUnit Process isolation caches constants, but not function declarations.
  13. * So we need to check if the constants are defined separately from
  14. * the functions to enable supporting process isolation in userland
  15. * code.
  16. */
  17. define('PASSWORD_BCRYPT', 1);
  18. define('PASSWORD_DEFAULT', PASSWORD_BCRYPT);
  19. define('PASSWORD_BCRYPT_DEFAULT_COST', 10);
  20. }
  21. if (!function_exists('password_hash')) {
  22. /**
  23. * Hash the password using the specified algorithm
  24. *
  25. * @param string $password The password to hash
  26. * @param int $algo The algorithm to use (Defined by PASSWORD_* constants)
  27. * @param array $options The options for the algorithm to use
  28. *
  29. * @return string|false The hashed password, or false on error.
  30. */
  31. function password_hash($password, $algo, array $options = array())
  32. {
  33. if (!function_exists('crypt')) {
  34. trigger_error("Crypt must be loaded for password_hash to function", E_USER_WARNING);
  35. return null;
  36. }
  37. if (is_null($password) || is_int($password)) {
  38. $password = (string)$password;
  39. }
  40. if (!is_string($password)) {
  41. trigger_error("password_hash(): Password must be a string", E_USER_WARNING);
  42. return null;
  43. }
  44. if (!is_int($algo)) {
  45. trigger_error("password_hash() expects parameter 2 to be long, " . gettype($algo) . " given", E_USER_WARNING);
  46. return null;
  47. }
  48. $resultLength = 0;
  49. switch ($algo) {
  50. case PASSWORD_BCRYPT:
  51. $cost = PASSWORD_BCRYPT_DEFAULT_COST;
  52. if (isset($options['cost'])) {
  53. $cost = $options['cost'];
  54. if ($cost < 4 || $cost > 31) {
  55. trigger_error(sprintf("password_hash(): Invalid bcrypt cost parameter specified: %d", $cost), E_USER_WARNING);
  56. return null;
  57. }
  58. }
  59. // The length of salt to generate
  60. $raw_salt_len = 16;
  61. // The length required in the final serialization
  62. $required_salt_len = 22;
  63. $hash_format = sprintf("$2y$%02d$", $cost);
  64. // The expected length of the final crypt() output
  65. $resultLength = 60;
  66. break;
  67. default:
  68. trigger_error(sprintf("password_hash(): Unknown password hashing algorithm: %s", $algo), E_USER_WARNING);
  69. return null;
  70. }
  71. $salt_requires_encoding = false;
  72. if (isset($options['salt'])) {
  73. switch (gettype($options['salt'])) {
  74. case 'NULL':
  75. case 'boolean':
  76. case 'integer':
  77. case 'double':
  78. case 'string':
  79. $salt = (string)$options['salt'];
  80. break;
  81. case 'object':
  82. if (method_exists($options['salt'], '__tostring')) {
  83. $salt = (string)$options['salt'];
  84. break;
  85. }
  86. case 'array':
  87. case 'resource':
  88. default:
  89. trigger_error('password_hash(): Non-string salt parameter supplied', E_USER_WARNING);
  90. return null;
  91. }
  92. if (PasswordCompat\binary\_strlen($salt) < $required_salt_len) {
  93. trigger_error(sprintf("password_hash(): Provided salt is too short: %d expecting %d", PasswordCompat\binary\_strlen($salt), $required_salt_len), E_USER_WARNING);
  94. return null;
  95. } elseif (0 == preg_match('#^[a-zA-Z0-9./]+$#D', $salt)) {
  96. $salt_requires_encoding = true;
  97. }
  98. } else {
  99. $buffer = '';
  100. $buffer_valid = false;
  101. if (function_exists('mcrypt_create_iv') && !defined('PHALANGER')) {
  102. $buffer = mcrypt_create_iv($raw_salt_len, MCRYPT_DEV_URANDOM);
  103. if ($buffer) {
  104. $buffer_valid = true;
  105. }
  106. }
  107. if (!$buffer_valid && function_exists('openssl_random_pseudo_bytes')) {
  108. $buffer = openssl_random_pseudo_bytes($raw_salt_len);
  109. if ($buffer) {
  110. $buffer_valid = true;
  111. }
  112. }
  113. if (!$buffer_valid && @is_readable('/dev/urandom')) {
  114. $f = fopen('/dev/urandom', 'r');
  115. $read = PasswordCompat\binary\_strlen($buffer);
  116. while ($read < $raw_salt_len) {
  117. $buffer .= fread($f, $raw_salt_len - $read);
  118. $read = PasswordCompat\binary\_strlen($buffer);
  119. }
  120. fclose($f);
  121. if ($read >= $raw_salt_len) {
  122. $buffer_valid = true;
  123. }
  124. }
  125. if (!$buffer_valid || PasswordCompat\binary\_strlen($buffer) < $raw_salt_len) {
  126. $bl = PasswordCompat\binary\_strlen($buffer);
  127. for ($i = 0; $i < $raw_salt_len; $i++) {
  128. if ($i < $bl) {
  129. $buffer[$i] = $buffer[$i] ^ chr(mt_rand(0, 255));
  130. } else {
  131. $buffer .= chr(mt_rand(0, 255));
  132. }
  133. }
  134. }
  135. $salt = $buffer;
  136. $salt_requires_encoding = true;
  137. }
  138. if ($salt_requires_encoding) {
  139. // encode string with the Base64 variant used by crypt
  140. $base64_digits =
  141. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  142. $bcrypt64_digits =
  143. './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  144. $base64_string = base64_encode($salt);
  145. $salt = strtr(rtrim($base64_string, '='), $base64_digits, $bcrypt64_digits);
  146. }
  147. $salt = PasswordCompat\binary\_substr($salt, 0, $required_salt_len);
  148. $hash = $hash_format . $salt;
  149. $ret = crypt($password, $hash);
  150. if (!is_string($ret) || PasswordCompat\binary\_strlen($ret) != $resultLength) {
  151. return false;
  152. }
  153. return $ret;
  154. }
  155. /**
  156. * Get information about the password hash. Returns an array of the information
  157. * that was used to generate the password hash.
  158. *
  159. * array(
  160. * 'algo' => 1,
  161. * 'algoName' => 'bcrypt',
  162. * 'options' => array(
  163. * 'cost' => PASSWORD_BCRYPT_DEFAULT_COST,
  164. * ),
  165. * )
  166. *
  167. * @param string $hash The password hash to extract info from
  168. *
  169. * @return array The array of information about the hash.
  170. */
  171. function password_get_info($hash)
  172. {
  173. $return = array(
  174. 'algo' => 0,
  175. 'algoName' => 'unknown',
  176. 'options' => array(),
  177. );
  178. if (PasswordCompat\binary\_substr($hash, 0, 4) == '$2y$' && PasswordCompat\binary\_strlen($hash) == 60) {
  179. $return['algo'] = PASSWORD_BCRYPT;
  180. $return['algoName'] = 'bcrypt';
  181. list($cost) = sscanf($hash, "$2y$%d$");
  182. $return['options']['cost'] = $cost;
  183. }
  184. return $return;
  185. }
  186. /**
  187. * Determine if the password hash needs to be rehashed according to the options provided
  188. *
  189. * If the answer is true, after validating the password using password_verify, rehash it.
  190. *
  191. * @param string $hash The hash to test
  192. * @param int $algo The algorithm used for new password hashes
  193. * @param array $options The options array passed to password_hash
  194. *
  195. * @return boolean True if the password needs to be rehashed.
  196. */
  197. function password_needs_rehash($hash, $algo, array $options = array())
  198. {
  199. $info = password_get_info($hash);
  200. if ($info['algo'] != $algo) {
  201. return true;
  202. }
  203. switch ($algo) {
  204. case PASSWORD_BCRYPT:
  205. $cost = isset($options['cost']) ? $options['cost'] : PASSWORD_BCRYPT_DEFAULT_COST;
  206. if ($cost != $info['options']['cost']) {
  207. return true;
  208. }
  209. break;
  210. }
  211. return false;
  212. }
  213. /**
  214. * Verify a password against a hash using a timing attack resistant approach
  215. *
  216. * @param string $password The password to verify
  217. * @param string $hash The hash to verify against
  218. *
  219. * @return boolean If the password matches the hash
  220. */
  221. function password_verify($password, $hash)
  222. {
  223. if (!function_exists('crypt')) {
  224. trigger_error("Crypt must be loaded for password_verify to function", E_USER_WARNING);
  225. return false;
  226. }
  227. $ret = crypt($password, $hash);
  228. if (!is_string($ret) || PasswordCompat\binary\_strlen($ret) != PasswordCompat\binary\_strlen($hash) || PasswordCompat\binary\_strlen($ret) <= 13) {
  229. return false;
  230. }
  231. $status = 0;
  232. for ($i = 0; $i < PasswordCompat\binary\_strlen($ret); $i++) {
  233. $status |= (ord($ret[$i]) ^ ord($hash[$i]));
  234. }
  235. return $status === 0;
  236. }
  237. }
  238. }
  239. namespace PasswordCompat\binary {
  240. if (!function_exists('PasswordCompat\\binary\\_strlen')) {
  241. /**
  242. * Count the number of bytes in a string
  243. *
  244. * We cannot simply use strlen() for this, because it might be overwritten by the mbstring extension.
  245. * In this case, strlen() will count the number of *characters* based on the internal encoding. A
  246. * sequence of bytes might be regarded as a single multibyte character.
  247. *
  248. * @param string $binary_string The input string
  249. *
  250. * @internal
  251. * @return int The number of bytes
  252. */
  253. function _strlen($binary_string)
  254. {
  255. if (function_exists('mb_strlen')) {
  256. return mb_strlen($binary_string, '8bit');
  257. }
  258. return strlen($binary_string);
  259. }
  260. /**
  261. * Get a substring based on byte limits
  262. *
  263. * @see _strlen()
  264. *
  265. * @param string $binary_string The input string
  266. * @param int $start
  267. * @param int $length
  268. *
  269. * @internal
  270. * @return string The substring
  271. */
  272. function _substr($binary_string, $start, $length)
  273. {
  274. if (function_exists('mb_substr')) {
  275. return mb_substr($binary_string, $start, $length, '8bit');
  276. }
  277. return substr($binary_string, $start, $length);
  278. }
  279. /**
  280. * Check if current PHP version is compatible with the library
  281. *
  282. * @return boolean the check result
  283. */
  284. function check()
  285. {
  286. static $pass = NULL;
  287. if (is_null($pass)) {
  288. if (function_exists('crypt')) {
  289. $hash = '$2y$04$usesomesillystringfore7hnbRJHxXVLeakoG8K30oukPsA.ztMG';
  290. $test = crypt("password", $hash);
  291. $pass = $test == $hash;
  292. } else {
  293. $pass = false;
  294. }
  295. }
  296. return $pass;
  297. }
  298. }
  299. }