crypto-sha1.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <!DOCTYPE html>
  2. <head>
  3. <!--
  4. Copyright (C) 2007 Apple Inc. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. -->
  25. <title>SunSpider crypto-sha1</title>
  26. </head>
  27. <body>
  28. <h3>crypto-sha1</h3>
  29. <div id="console">
  30. </div>
  31. <script>
  32. var _sunSpiderStartDate = new Date();
  33. /*
  34. * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  35. * in FIPS PUB 180-1
  36. * Version 2.1a Copyright Paul Johnston 2000 - 2002.
  37. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  38. * Distributed under the BSD License
  39. * See http://pajhome.org.uk/crypt/md5 for details.
  40. */
  41. /*
  42. * Configurable variables. You may need to tweak these to be compatible with
  43. * the server-side, but the defaults work in most cases.
  44. */
  45. var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  46. var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
  47. var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
  48. /*
  49. * These are the functions you'll usually want to call
  50. * They take string arguments and return either hex or base-64 encoded strings
  51. */
  52. function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
  53. function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
  54. function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
  55. function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
  56. function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
  57. function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
  58. /*
  59. * Perform a simple self-test to see if the VM is working
  60. */
  61. function sha1_vm_test()
  62. {
  63. return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
  64. }
  65. /*
  66. * Calculate the SHA-1 of an array of big-endian words, and a bit length
  67. */
  68. function core_sha1(x, len)
  69. {
  70. /* append padding */
  71. x[len >> 5] |= 0x80 << (24 - len % 32);
  72. x[((len + 64 >> 9) << 4) + 15] = len;
  73. var w = Array(80);
  74. var a = 1732584193;
  75. var b = -271733879;
  76. var c = -1732584194;
  77. var d = 271733878;
  78. var e = -1009589776;
  79. for(var i = 0; i < x.length; i += 16)
  80. {
  81. var olda = a;
  82. var oldb = b;
  83. var oldc = c;
  84. var oldd = d;
  85. var olde = e;
  86. for(var j = 0; j < 80; j++)
  87. {
  88. if(j < 16) w[j] = x[i + j];
  89. else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
  90. var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
  91. safe_add(safe_add(e, w[j]), sha1_kt(j)));
  92. e = d;
  93. d = c;
  94. c = rol(b, 30);
  95. b = a;
  96. a = t;
  97. }
  98. a = safe_add(a, olda);
  99. b = safe_add(b, oldb);
  100. c = safe_add(c, oldc);
  101. d = safe_add(d, oldd);
  102. e = safe_add(e, olde);
  103. }
  104. return Array(a, b, c, d, e);
  105. }
  106. /*
  107. * Perform the appropriate triplet combination function for the current
  108. * iteration
  109. */
  110. function sha1_ft(t, b, c, d)
  111. {
  112. if(t < 20) return (b & c) | ((~b) & d);
  113. if(t < 40) return b ^ c ^ d;
  114. if(t < 60) return (b & c) | (b & d) | (c & d);
  115. return b ^ c ^ d;
  116. }
  117. /*
  118. * Determine the appropriate additive constant for the current iteration
  119. */
  120. function sha1_kt(t)
  121. {
  122. return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
  123. (t < 60) ? -1894007588 : -899497514;
  124. }
  125. /*
  126. * Calculate the HMAC-SHA1 of a key and some data
  127. */
  128. function core_hmac_sha1(key, data)
  129. {
  130. var bkey = str2binb(key);
  131. if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
  132. var ipad = Array(16), opad = Array(16);
  133. for(var i = 0; i < 16; i++)
  134. {
  135. ipad[i] = bkey[i] ^ 0x36363636;
  136. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  137. }
  138. var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
  139. return core_sha1(opad.concat(hash), 512 + 160);
  140. }
  141. /*
  142. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  143. * to work around bugs in some JS interpreters.
  144. */
  145. function safe_add(x, y)
  146. {
  147. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  148. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  149. return (msw << 16) | (lsw & 0xFFFF);
  150. }
  151. /*
  152. * Bitwise rotate a 32-bit number to the left.
  153. */
  154. function rol(num, cnt)
  155. {
  156. return (num << cnt) | (num >>> (32 - cnt));
  157. }
  158. /*
  159. * Convert an 8-bit or 16-bit string to an array of big-endian words
  160. * In 8-bit function, characters >255 have their hi-byte silently ignored.
  161. */
  162. function str2binb(str)
  163. {
  164. var bin = Array();
  165. var mask = (1 << chrsz) - 1;
  166. for(var i = 0; i < str.length * chrsz; i += chrsz)
  167. bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
  168. return bin;
  169. }
  170. /*
  171. * Convert an array of big-endian words to a string
  172. */
  173. function binb2str(bin)
  174. {
  175. var str = "";
  176. var mask = (1 << chrsz) - 1;
  177. for(var i = 0; i < bin.length * 32; i += chrsz)
  178. str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
  179. return str;
  180. }
  181. /*
  182. * Convert an array of big-endian words to a hex string.
  183. */
  184. function binb2hex(binarray)
  185. {
  186. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  187. var str = "";
  188. for(var i = 0; i < binarray.length * 4; i++)
  189. {
  190. str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
  191. hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
  192. }
  193. return str;
  194. }
  195. /*
  196. * Convert an array of big-endian words to a base-64 string
  197. */
  198. function binb2b64(binarray)
  199. {
  200. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  201. var str = "";
  202. for(var i = 0; i < binarray.length * 4; i += 3)
  203. {
  204. var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16)
  205. | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
  206. | ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
  207. for(var j = 0; j < 4; j++)
  208. {
  209. if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
  210. else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
  211. }
  212. }
  213. return str;
  214. }
  215. var plainText = "Two households, both alike in dignity,\n\
  216. In fair Verona, where we lay our scene,\n\
  217. From ancient grudge break to new mutiny,\n\
  218. Where civil blood makes civil hands unclean.\n\
  219. From forth the fatal loins of these two foes\n\
  220. A pair of star-cross'd lovers take their life;\n\
  221. Whole misadventured piteous overthrows\n\
  222. Do with their death bury their parents' strife.\n\
  223. The fearful passage of their death-mark'd love,\n\
  224. And the continuance of their parents' rage,\n\
  225. Which, but their children's end, nought could remove,\n\
  226. Is now the two hours' traffic of our stage;\n\
  227. The which if you with patient ears attend,\n\
  228. What here shall miss, our toil shall strive to mend.";
  229. for (var i = 0; i <4; i++) {
  230. plainText += plainText;
  231. }
  232. var sha1Output = hex_sha1(plainText);
  233. var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
  234. document.getElementById("console").innerHTML = _sunSpiderInterval;
  235. </script>
  236. </body>
  237. </html>