4
Copyright (C) 2007 Apple Inc. All rights reserved.
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
9
1. Redistributions of source code must retain the above copyright
10
notice, this list of conditions and the following disclaimer.
11
2. Redistributions in binary form must reproduce the above copyright
12
notice, this list of conditions and the following disclaimer in the
13
documentation and/or other materials provided with the distribution.
15
THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
<title>SunSpider crypto-sha1</title>
39
var _sunSpiderStartDate = new Date();
42
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
44
* Version 2.1a Copyright Paul Johnston 2000 - 2002.
45
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
46
* Distributed under the BSD License
47
* See http://pajhome.org.uk/crypt/md5 for details.
51
* Configurable variables. You may need to tweak these to be compatible with
52
* the server-side, but the defaults work in most cases.
54
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
55
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
56
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
59
* These are the functions you'll usually want to call
60
* They take string arguments and return either hex or base-64 encoded strings
62
function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
63
function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
64
function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
65
function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
66
function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
67
function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
70
* Perform a simple self-test to see if the VM is working
72
function sha1_vm_test()
74
return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
78
* Calculate the SHA-1 of an array of big-endian words, and a bit length
80
function core_sha1(x, len)
83
x[len >> 5] |= 0x80 << (24 - len % 32);
84
x[((len + 64 >> 9) << 4) + 15] = len;
93
for(var i = 0; i < x.length; i += 16)
101
for(var j = 0; j < 80; j++)
103
if(j < 16) w[j] = x[i + j];
104
else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
105
var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
106
safe_add(safe_add(e, w[j]), sha1_kt(j)));
114
a = safe_add(a, olda);
115
b = safe_add(b, oldb);
116
c = safe_add(c, oldc);
117
d = safe_add(d, oldd);
118
e = safe_add(e, olde);
120
return Array(a, b, c, d, e);
125
* Perform the appropriate triplet combination function for the current
128
function sha1_ft(t, b, c, d)
130
if(t < 20) return (b & c) | ((~b) & d);
131
if(t < 40) return b ^ c ^ d;
132
if(t < 60) return (b & c) | (b & d) | (c & d);
137
* Determine the appropriate additive constant for the current iteration
141
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
142
(t < 60) ? -1894007588 : -899497514;
146
* Calculate the HMAC-SHA1 of a key and some data
148
function core_hmac_sha1(key, data)
150
var bkey = str2binb(key);
151
if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
153
var ipad = Array(16), opad = Array(16);
154
for(var i = 0; i < 16; i++)
156
ipad[i] = bkey[i] ^ 0x36363636;
157
opad[i] = bkey[i] ^ 0x5C5C5C5C;
160
var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
161
return core_sha1(opad.concat(hash), 512 + 160);
165
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
166
* to work around bugs in some JS interpreters.
168
function safe_add(x, y)
170
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
171
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
172
return (msw << 16) | (lsw & 0xFFFF);
176
* Bitwise rotate a 32-bit number to the left.
178
function rol(num, cnt)
180
return (num << cnt) | (num >>> (32 - cnt));
184
* Convert an 8-bit or 16-bit string to an array of big-endian words
185
* In 8-bit function, characters >255 have their hi-byte silently ignored.
187
function str2binb(str)
190
var mask = (1 << chrsz) - 1;
191
for(var i = 0; i < str.length * chrsz; i += chrsz)
192
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
197
* Convert an array of big-endian words to a string
199
function binb2str(bin)
202
var mask = (1 << chrsz) - 1;
203
for(var i = 0; i < bin.length * 32; i += chrsz)
204
str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
209
* Convert an array of big-endian words to a hex string.
211
function binb2hex(binarray)
213
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
215
for(var i = 0; i < binarray.length * 4; i++)
217
str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
218
hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
224
* Convert an array of big-endian words to a base-64 string
226
function binb2b64(binarray)
228
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
230
for(var i = 0; i < binarray.length * 4; i += 3)
232
var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16)
233
| (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
234
| ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
235
for(var j = 0; j < 4; j++)
237
if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
238
else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
245
var plainText = "Two households, both alike in dignity,\n\
246
In fair Verona, where we lay our scene,\n\
247
From ancient grudge break to new mutiny,\n\
248
Where civil blood makes civil hands unclean.\n\
249
From forth the fatal loins of these two foes\n\
250
A pair of star-cross'd lovers take their life;\n\
251
Whole misadventured piteous overthrows\n\
252
Do with their death bury their parents' strife.\n\
253
The fearful passage of their death-mark'd love,\n\
254
And the continuance of their parents' rage,\n\
255
Which, but their children's end, nought could remove,\n\
256
Is now the two hours' traffic of our stage;\n\
257
The which if you with patient ears attend,\n\
258
What here shall miss, our toil shall strive to mend.";
260
for (var i = 0; i <4; i++) {
261
plainText += plainText;
264
var sha1Output = hex_sha1(plainText);
267
var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
269
document.getElementById("console").innerHTML = _sunSpiderInterval;