PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/compat/password.php

https://gitlab.com/Anas7232/Layout-Changes
PHP | 251 lines | 136 code | 26 blank | 89 comment | 21 complexity | 5c10a76bbccf14514a54503510c74f84 MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 3.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * PHP ext/standard/password compatibility package
  41. *
  42. * @package CodeIgniter
  43. * @subpackage CodeIgniter
  44. * @category Compatibility
  45. * @author Andrey Andreev
  46. * @link https://codeigniter.com/user_guide/
  47. * @link http://php.net/password
  48. */
  49. // ------------------------------------------------------------------------
  50. if (is_php('5.5') OR ! defined('CRYPT_BLOWFISH') OR CRYPT_BLOWFISH !== 1 OR defined('HHVM_VERSION'))
  51. {
  52. return;
  53. }
  54. // ------------------------------------------------------------------------
  55. defined('PASSWORD_BCRYPT') OR define('PASSWORD_BCRYPT', 1);
  56. defined('PASSWORD_DEFAULT') OR define('PASSWORD_DEFAULT', PASSWORD_BCRYPT);
  57. // ------------------------------------------------------------------------
  58. if ( ! function_exists('password_get_info'))
  59. {
  60. /**
  61. * password_get_info()
  62. *
  63. * @link http://php.net/password_get_info
  64. * @param string $hash
  65. * @return array
  66. */
  67. function password_get_info($hash)
  68. {
  69. return (strlen($hash) < 60 OR sscanf($hash, '$2y$%d', $hash) !== 1)
  70. ? array('algo' => 0, 'algoName' => 'unknown', 'options' => array())
  71. : array('algo' => 1, 'algoName' => 'bcrypt', 'options' => array('cost' => $hash));
  72. }
  73. }
  74. // ------------------------------------------------------------------------
  75. if ( ! function_exists('password_hash'))
  76. {
  77. /**
  78. * password_hash()
  79. *
  80. * @link http://php.net/password_hash
  81. * @param string $password
  82. * @param int $algo
  83. * @param array $options
  84. * @return mixed
  85. */
  86. function password_hash($password, $algo, array $options = array())
  87. {
  88. static $func_overload;
  89. isset($func_overload) OR $func_overload = (extension_loaded('mbstring') && ini_get('mbstring.func_overload'));
  90. if ($algo !== 1)
  91. {
  92. trigger_error('password_hash(): Unknown hashing algorithm: '.(int) $algo, E_USER_WARNING);
  93. return NULL;
  94. }
  95. if (isset($options['cost']) && ($options['cost'] < 4 OR $options['cost'] > 31))
  96. {
  97. trigger_error('password_hash(): Invalid bcrypt cost parameter specified: '.(int) $options['cost'], E_USER_WARNING);
  98. return NULL;
  99. }
  100. if (isset($options['salt']) && ($saltlen = ($func_overload ? mb_strlen($options['salt'], '8bit') : strlen($options['salt']))) < 22)
  101. {
  102. trigger_error('password_hash(): Provided salt is too short: '.$saltlen.' expecting 22', E_USER_WARNING);
  103. return NULL;
  104. }
  105. elseif ( ! isset($options['salt']))
  106. {
  107. if (function_exists('random_bytes'))
  108. {
  109. try
  110. {
  111. $options['salt'] = random_bytes(16);
  112. }
  113. catch (Exception $e)
  114. {
  115. log_message('error', 'compat/password: Error while trying to use random_bytes(): '.$e->getMessage());
  116. return FALSE;
  117. }
  118. }
  119. elseif (defined('MCRYPT_DEV_URANDOM'))
  120. {
  121. $options['salt'] = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
  122. }
  123. elseif (DIRECTORY_SEPARATOR === '/' && (is_readable($dev = '/dev/arandom') OR is_readable($dev = '/dev/urandom')))
  124. {
  125. if (($fp = fopen($dev, 'rb')) === FALSE)
  126. {
  127. log_message('error', 'compat/password: Unable to open '.$dev.' for reading.');
  128. return FALSE;
  129. }
  130. // Try not to waste entropy ...
  131. is_php('5.4') && stream_set_chunk_size($fp, 16);
  132. $options['salt'] = '';
  133. for ($read = 0; $read < 16; $read = ($func_overload) ? mb_strlen($options['salt'], '8bit') : strlen($options['salt']))
  134. {
  135. if (($read = fread($fp, 16 - $read)) === FALSE)
  136. {
  137. log_message('error', 'compat/password: Error while reading from '.$dev.'.');
  138. return FALSE;
  139. }
  140. $options['salt'] .= $read;
  141. }
  142. fclose($fp);
  143. }
  144. elseif (function_exists('openssl_random_pseudo_bytes'))
  145. {
  146. $is_secure = NULL;
  147. $options['salt'] = openssl_random_pseudo_bytes(16, $is_secure);
  148. if ($is_secure !== TRUE)
  149. {
  150. log_message('error', 'compat/password: openssl_random_pseudo_bytes() set the $cryto_strong flag to FALSE');
  151. return FALSE;
  152. }
  153. }
  154. else
  155. {
  156. log_message('error', 'compat/password: No CSPRNG available.');
  157. return FALSE;
  158. }
  159. $options['salt'] = str_replace('+', '.', rtrim(base64_encode($options['salt']), '='));
  160. }
  161. elseif ( ! preg_match('#^[a-zA-Z0-9./]+$#D', $options['salt']))
  162. {
  163. $options['salt'] = str_replace('+', '.', rtrim(base64_encode($options['salt']), '='));
  164. }
  165. isset($options['cost']) OR $options['cost'] = 10;
  166. return (strlen($password = crypt($password, sprintf('$2y$%02d$%s', $options['cost'], $options['salt']))) === 60)
  167. ? $password
  168. : FALSE;
  169. }
  170. }
  171. // ------------------------------------------------------------------------
  172. if ( ! function_exists('password_needs_rehash'))
  173. {
  174. /**
  175. * password_needs_rehash()
  176. *
  177. * @link http://php.net/password_needs_rehash
  178. * @param string $hash
  179. * @param int $algo
  180. * @param array $options
  181. * @return bool
  182. */
  183. function password_needs_rehash($hash, $algo, array $options = array())
  184. {
  185. $info = password_get_info($hash);
  186. if ($algo !== $info['algo'])
  187. {
  188. return TRUE;
  189. }
  190. elseif ($algo === 1)
  191. {
  192. $options['cost'] = isset($options['cost']) ? (int) $options['cost'] : 10;
  193. return ($info['options']['cost'] !== $options['cost']);
  194. }
  195. // Odd at first glance, but according to a comment in PHP's own unit tests,
  196. // because it is an unknown algorithm - it's valid and therefore doesn't
  197. // need rehashing.
  198. return FALSE;
  199. }
  200. }
  201. // ------------------------------------------------------------------------
  202. if ( ! function_exists('password_verify'))
  203. {
  204. /**
  205. * password_verify()
  206. *
  207. * @link http://php.net/password_verify
  208. * @param string $password
  209. * @param string $hash
  210. * @return bool
  211. */
  212. function password_verify($password, $hash)
  213. {
  214. if (strlen($hash) !== 60 OR strlen($password = crypt($password, $hash)) !== 60)
  215. {
  216. return FALSE;
  217. }
  218. $compare = 0;
  219. for ($i = 0; $i < 60; $i++)
  220. {
  221. $compare |= (ord($password[$i]) ^ ord($hash[$i]));
  222. }
  223. return ($compare === 0);
  224. }
  225. }