Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / public / sha1.js @ c6f2b274

History | View | Annotate | Download (5.4 kB)

1 a52ea5e4 pastith
/*
2 a52ea5e4 pastith
 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
3 a52ea5e4 pastith
 * in FIPS PUB 180-1
4 a52ea5e4 pastith
 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
5 a52ea5e4 pastith
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
6 a52ea5e4 pastith
 * Distributed under the BSD License
7 a52ea5e4 pastith
 * See http://pajhome.org.uk/crypt/md5 for details.
8 a52ea5e4 pastith
 */
9 a52ea5e4 pastith
10 a52ea5e4 pastith
/*
11 a52ea5e4 pastith
 * Configurable variables. You may need to tweak these to be compatible with
12 a52ea5e4 pastith
 * the server-side, but the defaults work in most cases.
13 a52ea5e4 pastith
 */
14 a52ea5e4 pastith
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
15 a52ea5e4 pastith
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
16 a52ea5e4 pastith
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */
17 a52ea5e4 pastith
18 a52ea5e4 pastith
/*
19 a52ea5e4 pastith
 * These are the functions you'll usually want to call
20 a52ea5e4 pastith
 * They take string arguments and return either hex or base-64 encoded strings
21 a52ea5e4 pastith
 */
22 a52ea5e4 pastith
function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
23 a52ea5e4 pastith
function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
24 a52ea5e4 pastith
function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
25 a52ea5e4 pastith
function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
26 a52ea5e4 pastith
function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
27 a52ea5e4 pastith
function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
28 a52ea5e4 pastith
29 a52ea5e4 pastith
/*
30 a52ea5e4 pastith
 * Perform a simple self-test to see if the VM is working
31 a52ea5e4 pastith
 */
32 a52ea5e4 pastith
function sha1_vm_test()
33 a52ea5e4 pastith
{
34 a52ea5e4 pastith
  return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
35 a52ea5e4 pastith
}
36 a52ea5e4 pastith
37 a52ea5e4 pastith
/*
38 a52ea5e4 pastith
 * Calculate the SHA-1 of an array of big-endian words, and a bit length
39 a52ea5e4 pastith
 */
40 a52ea5e4 pastith
function core_sha1(x, len)
41 a52ea5e4 pastith
{
42 a52ea5e4 pastith
  /* append padding */
43 a52ea5e4 pastith
  x[len >> 5] |= 0x80 << (24 - len % 32);
44 a52ea5e4 pastith
  x[((len + 64 >> 9) << 4) + 15] = len;
45 a52ea5e4 pastith
46 a52ea5e4 pastith
  var w = Array(80);
47 a52ea5e4 pastith
  var a =  1732584193;
48 a52ea5e4 pastith
  var b = -271733879;
49 a52ea5e4 pastith
  var c = -1732584194;
50 a52ea5e4 pastith
  var d =  271733878;
51 a52ea5e4 pastith
  var e = -1009589776;
52 a52ea5e4 pastith
53 a52ea5e4 pastith
  for(var i = 0; i < x.length; i += 16)
54 a52ea5e4 pastith
  {
55 a52ea5e4 pastith
    var olda = a;
56 a52ea5e4 pastith
    var oldb = b;
57 a52ea5e4 pastith
    var oldc = c;
58 a52ea5e4 pastith
    var oldd = d;
59 a52ea5e4 pastith
    var olde = e;
60 a52ea5e4 pastith
61 a52ea5e4 pastith
    for(var j = 0; j < 80; j++)
62 a52ea5e4 pastith
    {
63 a52ea5e4 pastith
      if(j < 16) w[j] = x[i + j];
64 a52ea5e4 pastith
      else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
65 a52ea5e4 pastith
      var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
66 a52ea5e4 pastith
                       safe_add(safe_add(e, w[j]), sha1_kt(j)));
67 a52ea5e4 pastith
      e = d;
68 a52ea5e4 pastith
      d = c;
69 a52ea5e4 pastith
      c = rol(b, 30);
70 a52ea5e4 pastith
      b = a;
71 a52ea5e4 pastith
      a = t;
72 a52ea5e4 pastith
    }
73 a52ea5e4 pastith
74 a52ea5e4 pastith
    a = safe_add(a, olda);
75 a52ea5e4 pastith
    b = safe_add(b, oldb);
76 a52ea5e4 pastith
    c = safe_add(c, oldc);
77 a52ea5e4 pastith
    d = safe_add(d, oldd);
78 a52ea5e4 pastith
    e = safe_add(e, olde);
79 a52ea5e4 pastith
  }
80 a52ea5e4 pastith
  return Array(a, b, c, d, e);
81 a52ea5e4 pastith
82 a52ea5e4 pastith
}
83 a52ea5e4 pastith
84 a52ea5e4 pastith
/*
85 a52ea5e4 pastith
 * Perform the appropriate triplet combination function for the current
86 a52ea5e4 pastith
 * iteration
87 a52ea5e4 pastith
 */
88 a52ea5e4 pastith
function sha1_ft(t, b, c, d)
89 a52ea5e4 pastith
{
90 a52ea5e4 pastith
  if(t < 20) return (b & c) | ((~b) & d);
91 a52ea5e4 pastith
  if(t < 40) return b ^ c ^ d;
92 a52ea5e4 pastith
  if(t < 60) return (b & c) | (b & d) | (c & d);
93 a52ea5e4 pastith
  return b ^ c ^ d;
94 a52ea5e4 pastith
}
95 a52ea5e4 pastith
96 a52ea5e4 pastith
/*
97 a52ea5e4 pastith
 * Determine the appropriate additive constant for the current iteration
98 a52ea5e4 pastith
 */
99 a52ea5e4 pastith
function sha1_kt(t)
100 a52ea5e4 pastith
{
101 a52ea5e4 pastith
  return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :
102 a52ea5e4 pastith
         (t < 60) ? -1894007588 : -899497514;
103 a52ea5e4 pastith
}
104 a52ea5e4 pastith
105 a52ea5e4 pastith
/*
106 a52ea5e4 pastith
 * Calculate the HMAC-SHA1 of a key and some data
107 a52ea5e4 pastith
 */
108 a52ea5e4 pastith
function core_hmac_sha1(key, data)
109 a52ea5e4 pastith
{
110 a52ea5e4 pastith
  var bkey = str2binb(key);
111 a52ea5e4 pastith
  if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
112 a52ea5e4 pastith
113 a52ea5e4 pastith
  var ipad = Array(16), opad = Array(16);
114 a52ea5e4 pastith
  for(var i = 0; i < 16; i++)
115 a52ea5e4 pastith
  {
116 a52ea5e4 pastith
    ipad[i] = bkey[i] ^ 0x36363636;
117 a52ea5e4 pastith
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
118 a52ea5e4 pastith
  }
119 a52ea5e4 pastith
120 a52ea5e4 pastith
  var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
121 a52ea5e4 pastith
  return core_sha1(opad.concat(hash), 512 + 160);
122 a52ea5e4 pastith
}
123 a52ea5e4 pastith
124 a52ea5e4 pastith
/*
125 a52ea5e4 pastith
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
126 a52ea5e4 pastith
 * to work around bugs in some JS interpreters.
127 a52ea5e4 pastith
 */
128 a52ea5e4 pastith
function safe_add(x, y)
129 a52ea5e4 pastith
{
130 a52ea5e4 pastith
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
131 a52ea5e4 pastith
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
132 a52ea5e4 pastith
  return (msw << 16) | (lsw & 0xFFFF);
133 a52ea5e4 pastith
}
134 a52ea5e4 pastith
135 a52ea5e4 pastith
/*
136 a52ea5e4 pastith
 * Bitwise rotate a 32-bit number to the left.
137 a52ea5e4 pastith
 */
138 a52ea5e4 pastith
function rol(num, cnt)
139 a52ea5e4 pastith
{
140 a52ea5e4 pastith
  return (num << cnt) | (num >>> (32 - cnt));
141 a52ea5e4 pastith
}
142 a52ea5e4 pastith
143 a52ea5e4 pastith
/*
144 a52ea5e4 pastith
 * Convert an 8-bit or 16-bit string to an array of big-endian words
145 a52ea5e4 pastith
 * In 8-bit function, characters >255 have their hi-byte silently ignored.
146 a52ea5e4 pastith
 */
147 a52ea5e4 pastith
function str2binb(str)
148 a52ea5e4 pastith
{
149 a52ea5e4 pastith
  var bin = Array();
150 a52ea5e4 pastith
  var mask = (1 << chrsz) - 1;
151 a52ea5e4 pastith
  for(var i = 0; i < str.length * chrsz; i += chrsz)
152 a52ea5e4 pastith
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
153 a52ea5e4 pastith
  return bin;
154 a52ea5e4 pastith
}
155 a52ea5e4 pastith
156 a52ea5e4 pastith
/*
157 a52ea5e4 pastith
 * Convert an array of big-endian words to a string
158 a52ea5e4 pastith
 */
159 a52ea5e4 pastith
function binb2str(bin)
160 a52ea5e4 pastith
{
161 a52ea5e4 pastith
  var str = "";
162 a52ea5e4 pastith
  var mask = (1 << chrsz) - 1;
163 a52ea5e4 pastith
  for(var i = 0; i < bin.length * 32; i += chrsz)
164 a52ea5e4 pastith
    str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
165 a52ea5e4 pastith
  return str;
166 a52ea5e4 pastith
}
167 a52ea5e4 pastith
168 a52ea5e4 pastith
/*
169 a52ea5e4 pastith
 * Convert an array of big-endian words to a hex string.
170 a52ea5e4 pastith
 */
171 a52ea5e4 pastith
function binb2hex(binarray)
172 a52ea5e4 pastith
{
173 a52ea5e4 pastith
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
174 a52ea5e4 pastith
  var str = "";
175 a52ea5e4 pastith
  for(var i = 0; i < binarray.length * 4; i++)
176 a52ea5e4 pastith
  {
177 a52ea5e4 pastith
    str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
178 a52ea5e4 pastith
           hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8  )) & 0xF);
179 a52ea5e4 pastith
  }
180 a52ea5e4 pastith
  return str;
181 a52ea5e4 pastith
}
182 a52ea5e4 pastith
183 a52ea5e4 pastith
/*
184 a52ea5e4 pastith
 * Convert an array of big-endian words to a base-64 string
185 a52ea5e4 pastith
 */
186 a52ea5e4 pastith
function binb2b64(binarray)
187 a52ea5e4 pastith
{
188 a52ea5e4 pastith
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
189 a52ea5e4 pastith
  var str = "";
190 a52ea5e4 pastith
  for(var i = 0; i < binarray.length * 4; i += 3)
191 a52ea5e4 pastith
  {
192 a52ea5e4 pastith
    var triplet = (((binarray[i   >> 2] >> 8 * (3 -  i   %4)) & 0xFF) << 16)
193 a52ea5e4 pastith
                | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
194 a52ea5e4 pastith
                |  ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
195 a52ea5e4 pastith
    for(var j = 0; j < 4; j++)
196 a52ea5e4 pastith
    {
197 a52ea5e4 pastith
      if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
198 a52ea5e4 pastith
      else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
199 a52ea5e4 pastith
    }
200 a52ea5e4 pastith
  }
201 a52ea5e4 pastith
  return str;
202 a52ea5e4 pastith
}