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
/*	Return to a port of Paul Johnstone's MD5 implementation
2
 *	http://pajhome.org.uk/crypt/md5/index.html
3
 *
4
 *	Copyright (C) Paul Johnston 1999 - 2002.
5
 *	Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
6
 * 	Distributed under the BSD License
7
 *
8
 *	Dojo port by Tom Trenka
9
 *
10
 *	2005-12-7
11
 *	All conversions are internalized (no dependencies)
12
 *	implemented getHMAC for message digest auth.
13
 */
14
 
15
dojo.require("dojo.crypto");
16
dojo.provide("dojo.crypto.MD5");
17
dojo.crypto.MD5 = 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(n, c) {
64
		return (n << c) | (n >>> (32 - c));
65
	}
66
	function C(q, a, b, x, s, t) {
67
		return add(R(add(add(a, q), add(x, t)), s), b);
68
	}
69
	function FF(a, b, c, d, x, s, t) {
70
		return C((b & c) | ((~b) & d), a, b, x, s, t);
71
	}
72
	function GG(a, b, c, d, x, s, t) {
73
		return C((b & d) | (c & (~d)), a, b, x, s, t);
74
	}
75
	function HH(a, b, c, d, x, s, t) {
76
		return C(b ^ c ^ d, a, b, x, s, t);
77
	}
78
	function II(a, b, c, d, x, s, t) {
79
		return C(c ^ (b | (~d)), a, b, x, s, t);
80
	}
81
	function core(x, len) {
82
		x[len >> 5] |= 128 << ((len) % 32);
83
		x[(((len + 64) >>> 9) << 4) + 14] = len;
84
		var a = 1732584193;
85
		var b = -271733879;
86
		var c = -1732584194;
87
		var d = 271733878;
88
		for (var i = 0; i < x.length; i += 16) {
89
			var olda = a;
90
			var oldb = b;
91
			var oldc = c;
92
			var oldd = d;
93
			a = FF(a, b, c, d, x[i + 0], 7, -680876936);
94
			d = FF(d, a, b, c, x[i + 1], 12, -389564586);
95
			c = FF(c, d, a, b, x[i + 2], 17, 606105819);
96
			b = FF(b, c, d, a, x[i + 3], 22, -1044525330);
97
			a = FF(a, b, c, d, x[i + 4], 7, -176418897);
98
			d = FF(d, a, b, c, x[i + 5], 12, 1200080426);
99
			c = FF(c, d, a, b, x[i + 6], 17, -1473231341);
100
			b = FF(b, c, d, a, x[i + 7], 22, -45705983);
101
			a = FF(a, b, c, d, x[i + 8], 7, 1770035416);
102
			d = FF(d, a, b, c, x[i + 9], 12, -1958414417);
103
			c = FF(c, d, a, b, x[i + 10], 17, -42063);
104
			b = FF(b, c, d, a, x[i + 11], 22, -1990404162);
105
			a = FF(a, b, c, d, x[i + 12], 7, 1804603682);
106
			d = FF(d, a, b, c, x[i + 13], 12, -40341101);
107
			c = FF(c, d, a, b, x[i + 14], 17, -1502002290);
108
			b = FF(b, c, d, a, x[i + 15], 22, 1236535329);
109
			a = GG(a, b, c, d, x[i + 1], 5, -165796510);
110
			d = GG(d, a, b, c, x[i + 6], 9, -1069501632);
111
			c = GG(c, d, a, b, x[i + 11], 14, 643717713);
112
			b = GG(b, c, d, a, x[i + 0], 20, -373897302);
113
			a = GG(a, b, c, d, x[i + 5], 5, -701558691);
114
			d = GG(d, a, b, c, x[i + 10], 9, 38016083);
115
			c = GG(c, d, a, b, x[i + 15], 14, -660478335);
116
			b = GG(b, c, d, a, x[i + 4], 20, -405537848);
117
			a = GG(a, b, c, d, x[i + 9], 5, 568446438);
118
			d = GG(d, a, b, c, x[i + 14], 9, -1019803690);
119
			c = GG(c, d, a, b, x[i + 3], 14, -187363961);
120
			b = GG(b, c, d, a, x[i + 8], 20, 1163531501);
121
			a = GG(a, b, c, d, x[i + 13], 5, -1444681467);
122
			d = GG(d, a, b, c, x[i + 2], 9, -51403784);
123
			c = GG(c, d, a, b, x[i + 7], 14, 1735328473);
124
			b = GG(b, c, d, a, x[i + 12], 20, -1926607734);
125
			a = HH(a, b, c, d, x[i + 5], 4, -378558);
126
			d = HH(d, a, b, c, x[i + 8], 11, -2022574463);
127
			c = HH(c, d, a, b, x[i + 11], 16, 1839030562);
128
			b = HH(b, c, d, a, x[i + 14], 23, -35309556);
129
			a = HH(a, b, c, d, x[i + 1], 4, -1530992060);
130
			d = HH(d, a, b, c, x[i + 4], 11, 1272893353);
131
			c = HH(c, d, a, b, x[i + 7], 16, -155497632);
132
			b = HH(b, c, d, a, x[i + 10], 23, -1094730640);
133
			a = HH(a, b, c, d, x[i + 13], 4, 681279174);
134
			d = HH(d, a, b, c, x[i + 0], 11, -358537222);
135
			c = HH(c, d, a, b, x[i + 3], 16, -722521979);
136
			b = HH(b, c, d, a, x[i + 6], 23, 76029189);
137
			a = HH(a, b, c, d, x[i + 9], 4, -640364487);
138
			d = HH(d, a, b, c, x[i + 12], 11, -421815835);
139
			c = HH(c, d, a, b, x[i + 15], 16, 530742520);
140
			b = HH(b, c, d, a, x[i + 2], 23, -995338651);
141
			a = II(a, b, c, d, x[i + 0], 6, -198630844);
142
			d = II(d, a, b, c, x[i + 7], 10, 1126891415);
143
			c = II(c, d, a, b, x[i + 14], 15, -1416354905);
144
			b = II(b, c, d, a, x[i + 5], 21, -57434055);
145
			a = II(a, b, c, d, x[i + 12], 6, 1700485571);
146
			d = II(d, a, b, c, x[i + 3], 10, -1894986606);
147
			c = II(c, d, a, b, x[i + 10], 15, -1051523);
148
			b = II(b, c, d, a, x[i + 1], 21, -2054922799);
149
			a = II(a, b, c, d, x[i + 8], 6, 1873313359);
150
			d = II(d, a, b, c, x[i + 15], 10, -30611744);
151
			c = II(c, d, a, b, x[i + 6], 15, -1560198380);
152
			b = II(b, c, d, a, x[i + 13], 21, 1309151649);
153
			a = II(a, b, c, d, x[i + 4], 6, -145523070);
154
			d = II(d, a, b, c, x[i + 11], 10, -1120210379);
155
			c = II(c, d, a, b, x[i + 2], 15, 718787259);
156
			b = II(b, c, d, a, x[i + 9], 21, -343485551);
157
			a = add(a, olda);
158
			b = add(b, oldb);
159
			c = add(c, oldc);
160
			d = add(d, oldd);
161
		}
162
		return [a, b, c, d];
163
	}
164
	function hmac(data, key) {
165
		var wa = toWord(key);
166
		if (wa.length > 16) {
167
			wa = core(wa, key.length * chrsz);
168
		}
169
		var l = [], r = [];
170
		for (var i = 0; i < 16; i++) {
171
			l[i] = wa[i] ^ 909522486;
172
			r[i] = wa[i] ^ 1549556828;
173
		}
174
		var h = core(l.concat(toWord(data)), 512 + data.length * chrsz);
175
		return core(r.concat(h), 640);
176
	}
177
	this.compute = function (data, outputType) {
178
		var out = outputType || dojo.crypto.outputTypes.Base64;
179
		switch (out) {
180
		  case dojo.crypto.outputTypes.Hex:
181
			return toHex(core(toWord(data), data.length * chrsz));
182
		  case dojo.crypto.outputTypes.String:
183
			return toString(core(toWord(data), data.length * chrsz));
184
		  default:
185
			return toBase64(core(toWord(data), data.length * chrsz));
186
		}
187
	};
188
	this.getHMAC = function (data, key, outputType) {
189
		var out = outputType || dojo.crypto.outputTypes.Base64;
190
		switch (out) {
191
		  case dojo.crypto.outputTypes.Hex:
192
			return toHex(hmac(data, key));
193
		  case dojo.crypto.outputTypes.String:
194
			return toString(hmac(data, key));
195
		  default:
196
			return toBase64(hmac(data, key));
197
		}
198
	};
199
}();
200