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

/southpark/php/jabberclass/class_SHA1Library.php

https://github.com/sigmonky/LivingRoom
PHP | 264 lines | 161 code | 27 blank | 76 comment | 20 complexity | 14d7d8021caffa8a9de1ae069e0f1748 MD5 | raw file
  1. <?
  2. /*
  3. * This file was contributed (in part or whole) by a third party, and is
  4. * released under a BSD-compatible free software license. Please see the
  5. * CREDITS and LICENSE sections below for details.
  6. *
  7. *****************************************************************************
  8. *
  9. * DETAILS
  10. *
  11. * A PHP implementation of the Secure Hash Algorithm, SHA-1, as defined in
  12. * FIPS PUB 180-1. This is used by Centova only when using PHP
  13. * versions older than 4.3.0 (which did not support the sha1() function) and
  14. * the server does not have the mhash extension installed.
  15. *
  16. *
  17. * CREDITS/LICENSE
  18. *
  19. * Adjusted from the Javascript implementation by Joror (daan@parse.nl).
  20. *
  21. * Javascript Version 2.1 Copyright Paul Johnston 2000 - 2002.
  22. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  23. * Distributed under the BSD License
  24. * See http://pajhome.org.uk/crypt/md5 for details.
  25. *
  26. */
  27. class SHA1Library
  28. {
  29. /*
  30. * Configurable variables. You may need to tweak these to be compatible with
  31. * the server-side, but the defaults work in most cases.
  32. */
  33. var $hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  34. var $b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
  35. var $chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
  36. /*
  37. * These are the functions you'll usually want to call
  38. * They take string arguments and return either hex or base-64 encoded strings
  39. */
  40. function hex_sha1($s){return $this->binb2hex($this->core_sha1($this->str2binb($s),strlen($s) * $this->chrsz));}
  41. function b64_sha1($s){return $this->binb2b64($this->core_sha1($this->str2binb($s),strlen($s) * $this->chrsz));}
  42. function str_sha1($s){return $this->binb2str($this->core_sha1($this->str2binb($s),strlen($s) * $this->chrsz));}
  43. function hex_hmac_sha1($key, $data){ return $this->binb2hex($this->core_hmac_sha1($key, $data));}
  44. function b64_hmac_sha1($key, $data){ return $this->binb2b64($this->core_hmac_sha1($key, $data));}
  45. function str_hmac_sha1($key, $data){ return $this->binb2str($this->core_hmac_sha1($key, $data));}
  46. /*
  47. * Perform a simple self-test to see if the VM is working
  48. */
  49. function sha1_vm_test()
  50. {
  51. return $this->hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
  52. }
  53. /*
  54. * Calculate the SHA-1 of an array of big-endian words, and a bit $length
  55. */
  56. function core_sha1($x, $len)
  57. {
  58. /* append padding */
  59. $x[$len >> 5] |= 0x80 << (24 - $len % 32);
  60. $x[(($len + 64 >> 9) << 4) + 15] = $len;
  61. $w = Array();
  62. $a = 1732584193;
  63. $b = -271733879;
  64. $c = -1732584194;
  65. $d = 271733878;
  66. $e = -1009589776;
  67. for($i = 0; $i < sizeof($x); $i += 16)
  68. {
  69. $olda = $a;
  70. $oldb = $b;
  71. $oldc = $c;
  72. $oldd = $d;
  73. $olde = $e;
  74. for($j = 0; $j < 80; $j++)
  75. {
  76. if ($j < 16)
  77. $w[$j] = $x[$i + $j];
  78. else
  79. $w[$j] = $this->rol($w[$j-3] ^ $w[$j-8] ^ $w[$j-14] ^ $w[$j-16], 1);
  80. $t = $this->safe_add( $this->safe_add($this->rol($a, 5), $this->sha1_ft($j, $b, $c, $d)),
  81. $this->safe_add($this->safe_add($e, $w[$j]), $this->sha1_kt($j)));
  82. $e = $d;
  83. $d = $c;
  84. $c = $this->rol($b, 30);
  85. $b = $a;
  86. $a = $t;
  87. }
  88. $a = $this->safe_add($a, $olda);
  89. $b = $this->safe_add($b, $oldb);
  90. $c = $this->safe_add($c, $oldc);
  91. $d = $this->safe_add($d, $oldd);
  92. $e = $this->safe_add($e, $olde);
  93. }
  94. return Array($a, $b, $c, $d, $e);
  95. }
  96. /*
  97. * Joror: PHP does not have the java(script) >>> operator, so this is a
  98. * replacement function. Credits to Terium.
  99. */
  100. function zerofill_rightshift($a, $b)
  101. {
  102. $z = hexdec(80000000);
  103. if ($z & $a)
  104. {
  105. $a >>= 1;
  106. $a &= (~ $z);
  107. $a |= 0x40000000;
  108. $a >>= ($b-1);
  109. }
  110. else
  111. {
  112. $a >>= $b;
  113. }
  114. return $a;
  115. }
  116. /*
  117. * Perform the appropriate triplet combination function for the current
  118. * iteration
  119. */
  120. function sha1_ft($t, $b, $c, $d)
  121. {
  122. if($t < 20) return ($b & $c) | ((~$b) & $d);
  123. if($t < 40) return $b ^ $c ^ $d;
  124. if($t < 60) return ($b & $c) | ($b & $d) | ($c & $d);
  125. return $b ^ $c ^ $d;
  126. }
  127. /*
  128. * Determine the appropriate additive constant for the current iteration
  129. * Silly php does not understand the inline-if operator well when nested,
  130. * so that's why it's ()ed now.
  131. */
  132. function sha1_kt($t)
  133. {
  134. return ($t < 20) ? 1518500249 : (($t < 40) ? 1859775393 :
  135. (($t < 60) ? -1894007588 : -899497514));
  136. }
  137. /*
  138. * Calculate the HMAC-SHA1 of a key and some data
  139. */
  140. function core_hmac_sha1($key, $data)
  141. {
  142. $bkey = $this->str2binb($key);
  143. if(sizeof($bkey) > 16) $bkey = $this->core_sha1($bkey, sizeof($key) * $this->chrsz);
  144. $ipad = Array();
  145. $opad = Array();
  146. for($i = 0; $i < 16; $i++)
  147. {
  148. $ipad[$i] = $bkey[$i] ^ 0x36363636;
  149. $opad[$i] = $bkey[$i] ^ 0x5C5C5C5C;
  150. }
  151. $hash = $this->core_sha1(array_merge($ipad,$this->str2binb($data)), 512 + sizeof($data) * $this->chrsz);
  152. return $this->core_sha1(array_merge($opad,$hash), 512 + 160);
  153. }
  154. /*
  155. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  156. * to work around bugs in some JS interpreters.
  157. */
  158. function safe_add($x, $y)
  159. {
  160. $lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
  161. $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
  162. return ($msw << 16) | ($lsw & 0xFFFF);
  163. }
  164. /*
  165. * Bitwise rotate a 32-bit number to the left.
  166. */
  167. function rol($num, $cnt)
  168. {
  169. return ($num << $cnt) | $this->zerofill_rightshift($num, (32 - $cnt));
  170. }
  171. /*
  172. * Convert an 8-bit or 16-bit string to an array of big-endian words
  173. * In 8-bit function, characters >255 have their hi-byte silently ignored.
  174. */
  175. function str2binb($str)
  176. {
  177. $bin = Array();
  178. $mask = (1 << $this->chrsz) - 1;
  179. for($i = 0; $i < strlen($str) * $this->chrsz; $i += $this->chrsz)
  180. $bin[$i >> 5] |= (ord($str{$i / $this->chrsz}) & $mask) << (24 - $i%32);
  181. return $bin;
  182. }
  183. /*
  184. * Convert an array of big-endian words to a string
  185. */
  186. function binb2str($bin)
  187. {
  188. $str = "";
  189. $mask = (1 << $this->chrsz) - 1;
  190. for($i = 0; $i < sizeof($bin) * 32; $i += $this->chrsz)
  191. $str .= chr($this->zerofill_rightshift($bin[$i>>5], 24 - $i%32) & $mask);
  192. return $str;
  193. }
  194. /*
  195. * Convert an array of big-endian words to a hex string.
  196. */
  197. function binb2hex($binarray)
  198. {
  199. $hex_tab = $this->hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  200. $str = "";
  201. for($i = 0; $i < sizeof($binarray) * 4; $i++)
  202. {
  203. $str .= $hex_tab{($binarray[$i>>2] >> ((3 - $i%4)*8+4)) & 0xF} .
  204. $hex_tab{($binarray[$i>>2] >> ((3 - $i%4)*8 )) & 0xF};
  205. }
  206. return $str;
  207. }
  208. /*
  209. * Convert an array of big-endian words to a base-64 string
  210. */
  211. function binb2b64($binarray)
  212. {
  213. $tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  214. $str = "";
  215. for($i = 0; i < sizeof($binarray) * 4; $i += 3)
  216. {
  217. $triplet = ((($binarray[$i >> 2] >> 8 * (3 - $i %4)) & 0xFF) << 16)
  218. | ((($binarray[$i+1 >> 2] >> 8 * (3 - ($i+1)%4)) & 0xFF) << 8 )
  219. | (($binarray[$i+2 >> 2] >> 8 * (3 - ($i+2)%4)) & 0xFF);
  220. for($j = 0; $j < 4; $j++)
  221. {
  222. if($i * 8 + $j * 6 > sizeof($binarray) * 32) $str .= $this->b64pad;
  223. else $str .= $tab{($triplet >> 6*(3-j)) & 0x3F};
  224. }
  225. }
  226. return $str;
  227. }
  228. }
  229. if ( !function_exists('sha1') )
  230. {
  231. function sha1( $string, $raw_output = false )
  232. {
  233. $library = &new SHA1Library();
  234. return $raw_output ? $library->str_sha1($string) : $library->hex_sha1($string);
  235. }
  236. }
  237. ?>