js/src/devtools/jint/sunspider/crypto-sha1.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial