PageRenderTime 67ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Sha1.php

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