Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1422 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
1318 alexandre_ 4
 
1422 alexandre_ 5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
 
12
 
1318 alexandre_ 13
dojo.require("dojo.crypto");
14
dojo.provide("dojo.crypto.MD5");
15
dojo.crypto.MD5 = new function () {
16
	var chrsz = 8;
17
	var mask = (1 << chrsz) - 1;
18
	function toWord(s) {
19
		var wa = [];
20
		for (var i = 0; i < s.length * chrsz; i += chrsz) {
21
			wa[i >> 5] |= (s.charCodeAt(i / chrsz) & mask) << (i % 32);
22
		}
23
		return wa;
24
	}
25
	function toString(wa) {
26
		var s = [];
27
		for (var i = 0; i < wa.length * 32; i += chrsz) {
28
			s.push(String.fromCharCode((wa[i >> 5] >>> (i % 32)) & mask));
29
		}
30
		return s.join("");
31
	}
32
	function toHex(wa) {
33
		var h = "0123456789abcdef";
34
		var s = [];
35
		for (var i = 0; i < wa.length * 4; i++) {
36
			s.push(h.charAt((wa[i >> 2] >> ((i % 4) * 8 + 4)) & 15) + h.charAt((wa[i >> 2] >> ((i % 4) * 8)) & 15));
37
		}
38
		return s.join("");
39
	}
40
	function toBase64(wa) {
41
		var p = "=";
42
		var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
43
		var s = [];
44
		for (var i = 0; i < wa.length * 4; i += 3) {
45
			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);
46
			for (var j = 0; j < 4; j++) {
47
				if (i * 8 + j * 6 > wa.length * 32) {
48
					s.push(p);
49
				} else {
50
					s.push(tab.charAt((t >> 6 * (3 - j)) & 63));
51
				}
52
			}
53
		}
54
		return s.join("");
55
	}
56
	function add(x, y) {
57
		var l = (x & 65535) + (y & 65535);
58
		var m = (x >> 16) + (y >> 16) + (l >> 16);
59
		return (m << 16) | (l & 65535);
60
	}
61
	function R(n, c) {
62
		return (n << c) | (n >>> (32 - c));
63
	}
64
	function C(q, a, b, x, s, t) {
65
		return add(R(add(add(a, q), add(x, t)), s), b);
66
	}
67
	function FF(a, b, c, d, x, s, t) {
68
		return C((b & c) | ((~b) & d), a, b, x, s, t);
69
	}
70
	function GG(a, b, c, d, x, s, t) {
71
		return C((b & d) | (c & (~d)), a, b, x, s, t);
72
	}
73
	function HH(a, b, c, d, x, s, t) {
74
		return C(b ^ c ^ d, a, b, x, s, t);
75
	}
76
	function II(a, b, c, d, x, s, t) {
77
		return C(c ^ (b | (~d)), a, b, x, s, t);
78
	}
79
	function core(x, len) {
80
		x[len >> 5] |= 128 << ((len) % 32);
81
		x[(((len + 64) >>> 9) << 4) + 14] = len;
82
		var a = 1732584193;
83
		var b = -271733879;
84
		var c = -1732584194;
85
		var d = 271733878;
86
		for (var i = 0; i < x.length; i += 16) {
87
			var olda = a;
88
			var oldb = b;
89
			var oldc = c;
90
			var oldd = d;
91
			a = FF(a, b, c, d, x[i + 0], 7, -680876936);
92
			d = FF(d, a, b, c, x[i + 1], 12, -389564586);
93
			c = FF(c, d, a, b, x[i + 2], 17, 606105819);
94
			b = FF(b, c, d, a, x[i + 3], 22, -1044525330);
95
			a = FF(a, b, c, d, x[i + 4], 7, -176418897);
96
			d = FF(d, a, b, c, x[i + 5], 12, 1200080426);
97
			c = FF(c, d, a, b, x[i + 6], 17, -1473231341);
98
			b = FF(b, c, d, a, x[i + 7], 22, -45705983);
99
			a = FF(a, b, c, d, x[i + 8], 7, 1770035416);
100
			d = FF(d, a, b, c, x[i + 9], 12, -1958414417);
101
			c = FF(c, d, a, b, x[i + 10], 17, -42063);
102
			b = FF(b, c, d, a, x[i + 11], 22, -1990404162);
103
			a = FF(a, b, c, d, x[i + 12], 7, 1804603682);
104
			d = FF(d, a, b, c, x[i + 13], 12, -40341101);
105
			c = FF(c, d, a, b, x[i + 14], 17, -1502002290);
106
			b = FF(b, c, d, a, x[i + 15], 22, 1236535329);
107
			a = GG(a, b, c, d, x[i + 1], 5, -165796510);
108
			d = GG(d, a, b, c, x[i + 6], 9, -1069501632);
109
			c = GG(c, d, a, b, x[i + 11], 14, 643717713);
110
			b = GG(b, c, d, a, x[i + 0], 20, -373897302);
111
			a = GG(a, b, c, d, x[i + 5], 5, -701558691);
112
			d = GG(d, a, b, c, x[i + 10], 9, 38016083);
113
			c = GG(c, d, a, b, x[i + 15], 14, -660478335);
114
			b = GG(b, c, d, a, x[i + 4], 20, -405537848);
115
			a = GG(a, b, c, d, x[i + 9], 5, 568446438);
116
			d = GG(d, a, b, c, x[i + 14], 9, -1019803690);
117
			c = GG(c, d, a, b, x[i + 3], 14, -187363961);
118
			b = GG(b, c, d, a, x[i + 8], 20, 1163531501);
119
			a = GG(a, b, c, d, x[i + 13], 5, -1444681467);
120
			d = GG(d, a, b, c, x[i + 2], 9, -51403784);
121
			c = GG(c, d, a, b, x[i + 7], 14, 1735328473);
122
			b = GG(b, c, d, a, x[i + 12], 20, -1926607734);
123
			a = HH(a, b, c, d, x[i + 5], 4, -378558);
124
			d = HH(d, a, b, c, x[i + 8], 11, -2022574463);
125
			c = HH(c, d, a, b, x[i + 11], 16, 1839030562);
126
			b = HH(b, c, d, a, x[i + 14], 23, -35309556);
127
			a = HH(a, b, c, d, x[i + 1], 4, -1530992060);
128
			d = HH(d, a, b, c, x[i + 4], 11, 1272893353);
129
			c = HH(c, d, a, b, x[i + 7], 16, -155497632);
130
			b = HH(b, c, d, a, x[i + 10], 23, -1094730640);
131
			a = HH(a, b, c, d, x[i + 13], 4, 681279174);
132
			d = HH(d, a, b, c, x[i + 0], 11, -358537222);
133
			c = HH(c, d, a, b, x[i + 3], 16, -722521979);
134
			b = HH(b, c, d, a, x[i + 6], 23, 76029189);
135
			a = HH(a, b, c, d, x[i + 9], 4, -640364487);
136
			d = HH(d, a, b, c, x[i + 12], 11, -421815835);
137
			c = HH(c, d, a, b, x[i + 15], 16, 530742520);
138
			b = HH(b, c, d, a, x[i + 2], 23, -995338651);
139
			a = II(a, b, c, d, x[i + 0], 6, -198630844);
140
			d = II(d, a, b, c, x[i + 7], 10, 1126891415);
141
			c = II(c, d, a, b, x[i + 14], 15, -1416354905);
142
			b = II(b, c, d, a, x[i + 5], 21, -57434055);
143
			a = II(a, b, c, d, x[i + 12], 6, 1700485571);
144
			d = II(d, a, b, c, x[i + 3], 10, -1894986606);
145
			c = II(c, d, a, b, x[i + 10], 15, -1051523);
146
			b = II(b, c, d, a, x[i + 1], 21, -2054922799);
147
			a = II(a, b, c, d, x[i + 8], 6, 1873313359);
148
			d = II(d, a, b, c, x[i + 15], 10, -30611744);
149
			c = II(c, d, a, b, x[i + 6], 15, -1560198380);
150
			b = II(b, c, d, a, x[i + 13], 21, 1309151649);
151
			a = II(a, b, c, d, x[i + 4], 6, -145523070);
152
			d = II(d, a, b, c, x[i + 11], 10, -1120210379);
153
			c = II(c, d, a, b, x[i + 2], 15, 718787259);
154
			b = II(b, c, d, a, x[i + 9], 21, -343485551);
155
			a = add(a, olda);
156
			b = add(b, oldb);
157
			c = add(c, oldc);
158
			d = add(d, oldd);
159
		}
160
		return [a, b, c, d];
161
	}
162
	function hmac(data, key) {
163
		var wa = toWord(key);
164
		if (wa.length > 16) {
165
			wa = core(wa, key.length * chrsz);
166
		}
167
		var l = [], r = [];
168
		for (var i = 0; i < 16; i++) {
169
			l[i] = wa[i] ^ 909522486;
170
			r[i] = wa[i] ^ 1549556828;
171
		}
172
		var h = core(l.concat(toWord(data)), 512 + data.length * chrsz);
173
		return core(r.concat(h), 640);
174
	}
175
	this.compute = function (data, outputType) {
176
		var out = outputType || dojo.crypto.outputTypes.Base64;
177
		switch (out) {
178
		  case dojo.crypto.outputTypes.Hex:
179
			return toHex(core(toWord(data), data.length * chrsz));
180
		  case dojo.crypto.outputTypes.String:
181
			return toString(core(toWord(data), data.length * chrsz));
182
		  default:
183
			return toBase64(core(toWord(data), data.length * chrsz));
184
		}
185
	};
186
	this.getHMAC = function (data, key, outputType) {
187
		var out = outputType || dojo.crypto.outputTypes.Base64;
188
		switch (out) {
189
		  case dojo.crypto.outputTypes.Hex:
190
			return toHex(hmac(data, key));
191
		  case dojo.crypto.outputTypes.String:
192
			return toString(hmac(data, key));
193
		  default:
194
			return toBase64(hmac(data, key));
195
		}
196
	};
197
}();
198