crypto-sha1.js 8.0 KB

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