Sha1.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * SHA1 Encoding Class
  19. *
  20. * Purpose: Provides 160 bit hashing using The Secure Hash Algorithm
  21. * developed at the National Institute of Standards and Technology. The 40
  22. * character SHA1 message hash is computationally infeasible to crack.
  23. *
  24. * This class is a fallback for servers that are not running PHP greater than
  25. * 4.3, or do not have the MHASH library.
  26. *
  27. * This class is based on two scripts:
  28. *
  29. * Marcus Campbell's PHP implementation (GNU license)
  30. * http://www.tecknik.net/sha-1/
  31. *
  32. * ...which is based on Paul Johnston's JavaScript version
  33. * (BSD license). http://pajhome.org.uk/
  34. *
  35. * I encapsulated the functions and wrote one additional method to fix
  36. * a hex conversion bug. - Rick Ellis
  37. *
  38. * @package CodeIgniter
  39. * @subpackage Libraries
  40. * @category Encryption
  41. * @author EllisLab Dev Team
  42. * @link http://codeigniter.com/user_guide/general/encryption.html
  43. */
  44. class CI_SHA1 {
  45. public function __construct()
  46. {
  47. log_message('debug', "SHA1 Class Initialized");
  48. }
  49. /**
  50. * Generate the Hash
  51. *
  52. * @access public
  53. * @param string
  54. * @return string
  55. */
  56. function generate($str)
  57. {
  58. $n = ((strlen($str) + 8) >> 6) + 1;
  59. for ($i = 0; $i < $n * 16; $i++)
  60. {
  61. $x[$i] = 0;
  62. }
  63. for ($i = 0; $i < strlen($str); $i++)
  64. {
  65. $x[$i >> 2] |= ord(substr($str, $i, 1)) << (24 - ($i % 4) * 8);
  66. }
  67. $x[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8);
  68. $x[$n * 16 - 1] = strlen($str) * 8;
  69. $a = 1732584193;
  70. $b = -271733879;
  71. $c = -1732584194;
  72. $d = 271733878;
  73. $e = -1009589776;
  74. for ($i = 0; $i < count($x); $i += 16)
  75. {
  76. $olda = $a;
  77. $oldb = $b;
  78. $oldc = $c;
  79. $oldd = $d;
  80. $olde = $e;
  81. for ($j = 0; $j < 80; $j++)
  82. {
  83. if ($j < 16)
  84. {
  85. $w[$j] = $x[$i + $j];
  86. }
  87. else
  88. {
  89. $w[$j] = $this->_rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1);
  90. }
  91. $t = $this->_safe_add($this->_safe_add($this->_rol($a, 5), $this->_ft($j, $b, $c, $d)), $this->_safe_add($this->_safe_add($e, $w[$j]), $this->_kt($j)));
  92. $e = $d;
  93. $d = $c;
  94. $c = $this->_rol($b, 30);
  95. $b = $a;
  96. $a = $t;
  97. }
  98. $a = $this->_safe_add($a, $olda);
  99. $b = $this->_safe_add($b, $oldb);
  100. $c = $this->_safe_add($c, $oldc);
  101. $d = $this->_safe_add($d, $oldd);
  102. $e = $this->_safe_add($e, $olde);
  103. }
  104. return $this->_hex($a).$this->_hex($b).$this->_hex($c).$this->_hex($d).$this->_hex($e);
  105. }
  106. // --------------------------------------------------------------------
  107. /**
  108. * Convert a decimal to hex
  109. *
  110. * @access private
  111. * @param string
  112. * @return string
  113. */
  114. function _hex($str)
  115. {
  116. $str = dechex($str);
  117. if (strlen($str) == 7)
  118. {
  119. $str = '0'.$str;
  120. }
  121. return $str;
  122. }
  123. // --------------------------------------------------------------------
  124. /**
  125. * Return result based on iteration
  126. *
  127. * @access private
  128. * @return string
  129. */
  130. function _ft($t, $b, $c, $d)
  131. {
  132. if ($t < 20)
  133. return ($b & $c) | ((~$b) & $d);
  134. if ($t < 40)
  135. return $b ^ $c ^ $d;
  136. if ($t < 60)
  137. return ($b & $c) | ($b & $d) | ($c & $d);
  138. return $b ^ $c ^ $d;
  139. }
  140. // --------------------------------------------------------------------
  141. /**
  142. * Determine the additive constant
  143. *
  144. * @access private
  145. * @return string
  146. */
  147. function _kt($t)
  148. {
  149. if ($t < 20)
  150. {
  151. return 1518500249;
  152. }
  153. else if ($t < 40)
  154. {
  155. return 1859775393;
  156. }
  157. else if ($t < 60)
  158. {
  159. return -1894007588;
  160. }
  161. else
  162. {
  163. return -899497514;
  164. }
  165. }
  166. // --------------------------------------------------------------------
  167. /**
  168. * Add integers, wrapping at 2^32
  169. *
  170. * @access private
  171. * @return string
  172. */
  173. function _safe_add($x, $y)
  174. {
  175. $lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
  176. $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
  177. return ($msw << 16) | ($lsw & 0xFFFF);
  178. }
  179. // --------------------------------------------------------------------
  180. /**
  181. * Bitwise rotate a 32-bit number
  182. *
  183. * @access private
  184. * @return integer
  185. */
  186. function _rol($num, $cnt)
  187. {
  188. return ($num << $cnt) | $this->_zero_fill($num, 32 - $cnt);
  189. }
  190. // --------------------------------------------------------------------
  191. /**
  192. * Pad string with zero
  193. *
  194. * @access private
  195. * @return string
  196. */
  197. function _zero_fill($a, $b)
  198. {
  199. $bin = decbin($a);
  200. if (strlen($bin) < $b)
  201. {
  202. $bin = 0;
  203. }
  204. else
  205. {
  206. $bin = substr($bin, 0, strlen($bin) - $b);
  207. }
  208. for ($i=0; $i < $b; $i++)
  209. {
  210. $bin = "0".$bin;
  211. }
  212. return bindec($bin);
  213. }
  214. }
  215. // END CI_SHA
  216. /* End of file Sha1.php */
  217. /* Location: ./system/libraries/Sha1.php */