src/firefoxos/hashes/sha1.js

Mon, 22 Apr 2013 22:00:43 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Mon, 22 Apr 2013 22:00:43 +0200
changeset 0
6a0957738c54
permissions
-rw-r--r--

Import pristine sources of new project OTPWCalc.

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.1 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 hexsha1(s){return binb2hex(coresha1(str2binb(s),s.length * chrsz));}
michael@0 23 function b64sha1(s){return binb2b64(coresha1(str2binb(s),s.length * chrsz));}
michael@0 24 function strsha1(s){return binb2str(coresha1(str2binb(s),s.length * chrsz));}
michael@0 25 function hexhmacsha1(key, data){ return binb2hex(corehmacsha1(key, data));}
michael@0 26 function b64hmacsha1(key, data){ return binb2b64(corehmacsha1(key, data));}
michael@0 27 function strhmacsha1(key, data){ return binb2str(corehmacsha1(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 sha1vmtest()
michael@0 33 {
michael@0 34 return hexsha1("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 coresha1(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 for(var i = 0; i < x.length; i += 16)
michael@0 54 {
michael@0 55 var olda = a;
michael@0 56 var oldb = b;
michael@0 57 var oldc = c;
michael@0 58 var oldd = d;
michael@0 59 var olde = e;
michael@0 60
michael@0 61 for(var j = 0; j < 80; j++)
michael@0 62 {
michael@0 63 if(j < 16) w[j] = x[i + j];
michael@0 64 else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
michael@0 65 var t = safeadd(safeadd(rol(a, 5), sha1ft(j, b, c, d)),
michael@0 66 safeadd(safeadd(e, w[j]), sha1kt(j)));
michael@0 67 e = d;
michael@0 68 d = c;
michael@0 69 c = rol(b, 30);
michael@0 70 b = a;
michael@0 71 a = t;
michael@0 72 }
michael@0 73
michael@0 74 a = safeadd(a, olda);
michael@0 75 b = safeadd(b, oldb);
michael@0 76 c = safeadd(c, oldc);
michael@0 77 d = safeadd(d, oldd);
michael@0 78 e = safeadd(e, olde);
michael@0 79 }
michael@0 80 return Array(a, b, c, d, e);
michael@0 81
michael@0 82 }
michael@0 83
michael@0 84 /*
michael@0 85 * Perform the appropriate triplet combination function for the current
michael@0 86 * iteration
michael@0 87 */
michael@0 88 function sha1ft(t, b, c, d)
michael@0 89 {
michael@0 90 if(t < 20) return (b & c) | ((~b) & d);
michael@0 91 if(t < 40) return b ^ c ^ d;
michael@0 92 if(t < 60) return (b & c) | (b & d) | (c & d);
michael@0 93 return b ^ c ^ d;
michael@0 94 }
michael@0 95
michael@0 96 /*
michael@0 97 * Determine the appropriate additive constant for the current iteration
michael@0 98 */
michael@0 99 function sha1kt(t)
michael@0 100 {
michael@0 101 return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
michael@0 102 (t < 60) ? -1894007588 : -899497514;
michael@0 103 }
michael@0 104
michael@0 105 /*
michael@0 106 * Calculate the HMAC-SHA1 of a key and some data
michael@0 107 */
michael@0 108 function corehmacsha1(key, data)
michael@0 109 {
michael@0 110 var bkey = str2binb(key);
michael@0 111 if(bkey.length > 16) bkey = coresha1(bkey, key.length * chrsz);
michael@0 112
michael@0 113 var ipad = Array(16), opad = Array(16);
michael@0 114 for(var i = 0; i < 16; i++)
michael@0 115 {
michael@0 116 ipad[i] = bkey[i] ^ 0x36363636;
michael@0 117 opad[i] = bkey[i] ^ 0x5C5C5C5C;
michael@0 118 }
michael@0 119
michael@0 120 var hash = coresha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
michael@0 121 return coresha1(opad.concat(hash), 512 + 160);
michael@0 122 }
michael@0 123
michael@0 124 /*
michael@0 125 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
michael@0 126 * to work around bugs in some JS interpreters.
michael@0 127 */
michael@0 128 function safeadd(x, y)
michael@0 129 {
michael@0 130 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
michael@0 131 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
michael@0 132 return (msw << 16) | (lsw & 0xFFFF);
michael@0 133 }
michael@0 134
michael@0 135 /*
michael@0 136 * Bitwise rotate a 32-bit number to the left.
michael@0 137 */
michael@0 138 function rol(num, cnt)
michael@0 139 {
michael@0 140 return (num << cnt) | (num >>> (32 - cnt));
michael@0 141 }
michael@0 142
michael@0 143 /*
michael@0 144 * Convert an 8-bit or 16-bit string to an array of big-endian words
michael@0 145 * In 8-bit function, characters >255 have their hi-byte silently ignored.
michael@0 146 */
michael@0 147 function str2binb(str)
michael@0 148 {
michael@0 149 var bin = Array();
michael@0 150 var mask = (1 << chrsz) - 1;
michael@0 151 for(var i = 0; i < str.length * chrsz; i += chrsz)
michael@0 152 bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32);
michael@0 153 return bin;
michael@0 154 }
michael@0 155
michael@0 156 /*
michael@0 157 * Convert an array of big-endian words to a string
michael@0 158 */
michael@0 159 function binb2str(bin)
michael@0 160 {
michael@0 161 var str = "";
michael@0 162 var mask = (1 << chrsz) - 1;
michael@0 163 for(var i = 0; i < bin.length * 32; i += chrsz)
michael@0 164 str += String.fromCharCode((bin[i>>5] >>> (24 - i%32)) & mask);
michael@0 165 return str;
michael@0 166 }
michael@0 167
michael@0 168 /*
michael@0 169 * Convert an array of big-endian words to a hex string.
michael@0 170 */
michael@0 171 function binb2hex(binarray)
michael@0 172 {
michael@0 173 var hextab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
michael@0 174 var str = "";
michael@0 175 for(var i = 0; i < binarray.length * 4; i++)
michael@0 176 {
michael@0 177 str += hextab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
michael@0 178 hextab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
michael@0 179 }
michael@0 180 return str;
michael@0 181 }
michael@0 182
michael@0 183 /*
michael@0 184 * Convert an array of big-endian words to a base-64 string
michael@0 185 */
michael@0 186 function binb2b64(binarray)
michael@0 187 {
michael@0 188 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
michael@0 189 var str = "";
michael@0 190 for(var i = 0; i < binarray.length * 4; i += 3)
michael@0 191 {
michael@0 192 var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16)
michael@0 193 | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
michael@0 194 | ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
michael@0 195 for(var j = 0; j < 4; j++)
michael@0 196 {
michael@0 197 if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
michael@0 198 else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
michael@0 199 }
michael@0 200 }
michael@0 201 return str;
michael@0 202 }

mercurial