PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendorci/core/compat/hash.php

https://gitlab.com/gundambison/secure
PHP | 198 lines | 105 code | 21 blank | 72 comment | 22 complexity | bd4dc4d286e1b3b831d23274ff2d2a52 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 - 2015, 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. (http://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link http://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/hash compatibility package
  41. *
  42. * @package CodeIgniter
  43. * @subpackage CodeIgniter
  44. * @category Compatibility
  45. * @author Andrey Andreev
  46. * @link http://codeigniter.com/user_guide/
  47. * @link http://php.net/hash
  48. */
  49. // ------------------------------------------------------------------------
  50. if (is_php('5.6'))
  51. {
  52. return;
  53. }
  54. // ------------------------------------------------------------------------
  55. if ( ! function_exists('hash_equals'))
  56. {
  57. /**
  58. * hash_equals()
  59. *
  60. * @link http://php.net/hash_equals
  61. * @param string $known_string
  62. * @param string $user_string
  63. * @return bool
  64. */
  65. function hash_equals($known_string, $user_string)
  66. {
  67. if ( ! is_string($known_string))
  68. {
  69. trigger_error('hash_equals(): Expected known_string to be a string, '.strtolower(gettype($known_string)).' given', E_USER_WARNING);
  70. return FALSE;
  71. }
  72. elseif ( ! is_string($user_string))
  73. {
  74. trigger_error('hash_equals(): Expected user_string to be a string, '.strtolower(gettype($user_string)).' given', E_USER_WARNING);
  75. return FALSE;
  76. }
  77. elseif (($length = strlen($known_string)) !== strlen($user_string))
  78. {
  79. return FALSE;
  80. }
  81. $diff = 0;
  82. for ($i = 0; $i < $length; $i++)
  83. {
  84. $diff |= ord($known_string[$i]) ^ ord($user_string[$i]);
  85. }
  86. return ($diff === 0);
  87. }
  88. }
  89. // ------------------------------------------------------------------------
  90. if (is_php('5.5'))
  91. {
  92. return;
  93. }
  94. // ------------------------------------------------------------------------
  95. if ( ! function_exists('hash_pbkdf2'))
  96. {
  97. /**
  98. * hash_pbkdf2()
  99. *
  100. * @link http://php.net/hash_pbkdf2
  101. * @param string $algo
  102. * @param string $password
  103. * @param string $salt
  104. * @param int $iterations
  105. * @param int $length
  106. * @param bool $raw_output
  107. * @return string
  108. */
  109. function hash_pbkdf2($algo, $password, $salt, $iterations, $length = 0, $raw_output = FALSE)
  110. {
  111. if ( ! in_array($algo, hash_algos(), TRUE))
  112. {
  113. trigger_error('hash_pbkdf2(): Unknown hashing algorithm: '.$algo, E_USER_WARNING);
  114. return FALSE;
  115. }
  116. if (($type = gettype($iterations)) !== 'integer')
  117. {
  118. if ($type === 'object' && method_exists($iterations, '__toString'))
  119. {
  120. $iterations = (string) $iterations;
  121. }
  122. if (is_string($iterations) && is_numeric($iterations))
  123. {
  124. $iterations = (int) $iterations;
  125. }
  126. else
  127. {
  128. trigger_error('hash_pbkdf2() expects parameter 4 to be long, '.$type.' given', E_USER_WARNING);
  129. return NULL;
  130. }
  131. }
  132. if ($iterations < 1)
  133. {
  134. trigger_error('hash_pbkdf2(): Iterations must be a positive integer: '.$iterations, E_USER_WARNING);
  135. return FALSE;
  136. }
  137. if (($type = gettype($length)) !== 'integer')
  138. {
  139. if ($type === 'object' && method_exists($length, '__toString'))
  140. {
  141. $length = (string) $length;
  142. }
  143. if (is_string($length) && is_numeric($length))
  144. {
  145. $length = (int) $length;
  146. }
  147. else
  148. {
  149. trigger_error('hash_pbkdf2() expects parameter 5 to be long, '.$type.' given', E_USER_WARNING);
  150. return NULL;
  151. }
  152. }
  153. if ($length < 0)
  154. {
  155. trigger_error('hash_pbkdf2(): Length must be greater than or equal to 0: '.$length, E_USER_WARNING);
  156. return FALSE;
  157. }
  158. $hash_length = strlen(hash($algo, NULL, TRUE));
  159. if (empty($length))
  160. {
  161. $length = $hash_length;
  162. }
  163. $hash = '';
  164. // Note: Blocks are NOT 0-indexed
  165. for ($bc = ceil($length / $hash_length), $bi = 1; $bi <= $bc; $bi++)
  166. {
  167. $key = $derived_key = hash_hmac($algo, $salt.pack('N', $bi), $password, TRUE);
  168. for ($i = 1; $i < $iterations; $i++)
  169. {
  170. $derived_key ^= $key = hash_hmac($algo, $key, $password, TRUE);
  171. }
  172. $hash .= $derived_key;
  173. }
  174. // This is not RFC-compatible, but we're aiming for natural PHP compatibility
  175. return substr($raw_output ? $hash : bin2hex($hash), 0, $length);
  176. }
  177. }