Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
 *	A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
3
 *	in FIPS PUB 180-1
4
 *
5
 * 	Version 2.1a Copyright Paul Johnston 2000 - 2002.
6
 * 	Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
7
 * 	Distributed under the BSD License
8
 * 	See http://pajhome.org.uk/crypt/md5 for details.
9
 *
10
 *	Dojo port by Tom Trenka
11
 */
12
 
13
dojo.require("dojo.crypto");
14
dojo.provide("dojo.crypto.SHA1");
15
dojo.require("dojo.experimental");
16
dojo.experimental("dojo.crypto.SHA1");
17
dojo.crypto.SHA1 = new function () {
18
	var chrsz = 8;
19
	var mask = (1 << chrsz) - 1;
20
	function toWord(s) {
21
		var wa = [];
22
		for (var i = 0; i < s.length * chrsz; i += chrsz) {
23
			wa[i >> 5] |= (s.charCodeAt(i / chrsz) & mask) << (i % 32);
24
		}
25
		return wa;
26
	}
27
	function toString(wa) {
28
		var s = [];
29
		for (var i = 0; i < wa.length * 32; i += chrsz) {
30
			s.push(String.fromCharCode((wa[i >> 5] >>> (i % 32)) & mask));
31
		}
32
		return s.join("");
33
	}
34
	function toHex(wa) {
35
		var h = "0123456789abcdef";
36
		var s = [];
37
		for (var i = 0; i < wa.length * 4; i++) {
38
			s.push(h.charAt((wa[i >> 2] >> ((i % 4) * 8 + 4)) & 15) + h.charAt((wa[i >> 2] >> ((i % 4) * 8)) & 15));
39
		}
40
		return s.join("");
41
	}
42
	function toBase64(wa) {
43
		var p = "=";
44
		var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
45
		var s = [];
46
		for (var i = 0; i < wa.length * 4; i += 3) {
47
			var t = (((wa[i >> 2] >> 8 * (i % 4)) & 255) << 16) | (((wa[i + 1 >> 2] >> 8 * ((i + 1) % 4)) & 255) << 8) | ((wa[i + 2 >> 2] >> 8 * ((i + 2) % 4)) & 255);
48
			for (var j = 0; j < 4; j++) {
49
				if (i * 8 + j * 6 > wa.length * 32) {
50
					s.push(p);
51
				} else {
52
					s.push(tab.charAt((t >> 6 * (3 - j)) & 63));
53
				}
54
			}
55
		}
56
		return s.join("");
57
	}
58
	function add(x, y) {
59
		var l = (x & 65535) + (y & 65535);
60
		var m = (x >> 16) + (y >> 16) + (l >> 16);
61
		return (m << 16) | (l & 65535);
62
	}
63
	function r(x, n) {
64
		return (x << n) | (x >>> (32 - n));
65
	}
66
	function f(u, v, w) {
67
		return ((u & v) | (~u & w));
68
	}
69
	function g(u, v, w) {
70
		return ((u & v) | (u & w) | (v & w));
71
	}
72
	function h(u, v, w) {
73
		return (u ^ v ^ w);
74
	}
75
	function fn(i, u, v, w) {
76
		if (i < 20) {
77
			return f(u, v, w);
78
		}
79
		if (i < 40) {
80
			return h(u, v, w);
81
		}
82
		if (i < 60) {
83
			return g(u, v, w);
84
		}
85
		return h(u, v, w);
86
	}
87
	function cnst(i) {
88
		if (i < 20) {
89
			return 1518500249;
90
		}
91
		if (i < 40) {
92
			return 1859775393;
93
		}
94
		if (i < 60) {
95
			return -1894007588;
96
		}
97
		return -899497514;
98
	}
99
	function core(x, len) {
100
		x[len >> 5] |= 128 << (24 - len % 32);
101
		x[((len + 64 >> 9) << 4) + 15] = len;
102
		var w = [];
103
		var a = 1732584193;
104
		var b = -271733879;
105
		var c = -1732584194;
106
		var d = 271733878;
107
		var e = -1009589776;
108
		for (var i = 0; i < x.length; i += 16) {
109
			var olda = a;
110
			var oldb = b;
111
			var oldc = c;
112
			var oldd = d;
113
			var olde = e;
114
			for (var j = 0; j < 80; j++) {
115
				if (j < 16) {
116
					w[j] = x[i + j];
117
				} else {
118
					w[j] = r(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
119
				}
120
				var t = add(add(r(a, 5), fn(j, b, c, d)), add(add(e, w[j]), cnst(j)));
121
				e = d;
122
				d = c;
123
				c = r(b, 30);
124
				b = a;
125
				a = t;
126
			}
127
			a = add(a, olda);
128
			b = add(b, oldb);
129
			c = add(c, oldc);
130
			d = add(d, oldd);
131
			e = add(e, olde);
132
		}
133
		return [a, b, c, d, e];
134
	}
135
	function hmac(data, key) {
136
		var wa = toWord(key);
137
		if (wa.length > 16) {
138
			wa = core(wa, key.length * chrsz);
139
		}
140
		var l = [], r = [];
141
		for (var i = 0; i < 16; i++) {
142
			l[i] = wa[i] ^ 909522486;
143
			r[i] = wa[i] ^ 1549556828;
144
		}
145
		var h = core(l.concat(toWord(data)), 512 + data.length * chrsz);
146
		return core(r.concat(h), 640);
147
	}
148
	this.compute = function (data, outputType) {
149
		var out = outputType || dojo.crypto.outputTypes.Base64;
150
		switch (out) {
151
		  case dojo.crypto.outputTypes.Hex:
152
			return toHex(core(toWord(data), data.length * chrsz));
153
		  case dojo.crypto.outputTypes.String:
154
			return toString(core(toWord(data), data.length * chrsz));
155
		  default:
156
			return toBase64(core(toWord(data), data.length * chrsz));
157
		}
158
	};
159
	this.getHMAC = function (data, key, outputType) {
160
		var out = outputType || dojo.crypto.outputTypes.Base64;
161
		switch (out) {
162
		  case dojo.crypto.outputTypes.Hex:
163
			return toHex(hmac(data, key));
164
		  case dojo.crypto.outputTypes.String:
165
			return toString(hmac(data, key));
166
		  default:
167
			return toBase64(hmac(data, key));
168
		}
169
	};
170
}();
171