2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.crypto.MD5"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.crypto.MD5"] = true;
|
|
|
3 |
dojo.provide("dojox.crypto.MD5");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojox.crypto._base");
|
|
|
6 |
|
|
|
7 |
/* Return to a port of Paul Johnstone's MD5 implementation
|
|
|
8 |
* http://pajhome.org.uk/crypt/md5/index.html
|
|
|
9 |
*
|
|
|
10 |
* Copyright (C) Paul Johnston 1999 - 2002.
|
|
|
11 |
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
|
|
12 |
* Distributed under the BSD License
|
|
|
13 |
*
|
|
|
14 |
* Dojo port by Tom Trenka
|
|
|
15 |
*
|
|
|
16 |
* 2005-12-7
|
|
|
17 |
* All conversions are internalized (no dependencies)
|
|
|
18 |
* implemented getHMAC for message digest auth.
|
|
|
19 |
*/
|
|
|
20 |
dojox.crypto.MD5 = new function(){
|
|
|
21 |
// summary
|
|
|
22 |
// object for creating digests using the MD5 algorithm
|
|
|
23 |
var chrsz=8;
|
|
|
24 |
var mask=(1<<chrsz)-1;
|
|
|
25 |
function toWord(s) {
|
|
|
26 |
var wa=[];
|
|
|
27 |
for(var i=0; i<s.length*chrsz; i+=chrsz)
|
|
|
28 |
wa[i>>5]|=(s.charCodeAt(i/chrsz)&mask)<<(i%32);
|
|
|
29 |
return wa;
|
|
|
30 |
}
|
|
|
31 |
function toString(wa){
|
|
|
32 |
var s=[];
|
|
|
33 |
for(var i=0; i<wa.length*32; i+=chrsz)
|
|
|
34 |
s.push(String.fromCharCode((wa[i>>5]>>>(i%32))&mask));
|
|
|
35 |
return s.join("");
|
|
|
36 |
}
|
|
|
37 |
function toHex(wa) {
|
|
|
38 |
var h="0123456789abcdef";
|
|
|
39 |
var s=[];
|
|
|
40 |
for(var i=0; i<wa.length*4; i++){
|
|
|
41 |
s.push(h.charAt((wa[i>>2]>>((i%4)*8+4))&0xF)+h.charAt((wa[i>>2]>>((i%4)*8))&0xF));
|
|
|
42 |
}
|
|
|
43 |
return s.join("");
|
|
|
44 |
}
|
|
|
45 |
function toBase64(wa){
|
|
|
46 |
var p="=";
|
|
|
47 |
var tab="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
48 |
var s=[];
|
|
|
49 |
for(var i=0; i<wa.length*4; i+=3){
|
|
|
50 |
var t=(((wa[i>>2]>>8*(i%4))&0xFF)<<16)|(((wa[i+1>>2]>>8*((i+1)%4))&0xFF)<<8)|((wa[i+2>>2]>>8*((i+2)%4))&0xFF);
|
|
|
51 |
for(var j=0; j<4; j++){
|
|
|
52 |
if(i*8+j*6>wa.length*32) s.push(p);
|
|
|
53 |
else s.push(tab.charAt((t>>6*(3-j))&0x3F));
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
return s.join("");
|
|
|
57 |
}
|
|
|
58 |
function add(x,y) {
|
|
|
59 |
var l=(x&0xFFFF)+(y&0xFFFF);
|
|
|
60 |
var m=(x>>16)+(y>>16)+(l>>16);
|
|
|
61 |
return (m<<16)|(l&0xFFFF);
|
|
|
62 |
}
|
|
|
63 |
function R(n,c){ return (n<<c)|(n>>>(32-c)); }
|
|
|
64 |
function C(q,a,b,x,s,t){ return add(R(add(add(a,q),add(x,t)),s),b); }
|
|
|
65 |
function FF(a,b,c,d,x,s,t){ return C((b&c)|((~b)&d),a,b,x,s,t); }
|
|
|
66 |
function GG(a,b,c,d,x,s,t){ return C((b&d)|(c&(~d)),a,b,x,s,t); }
|
|
|
67 |
function HH(a,b,c,d,x,s,t){ return C(b^c^d,a,b,x,s,t); }
|
|
|
68 |
function II(a,b,c,d,x,s,t){ return C(c^(b|(~d)),a,b,x,s,t); }
|
|
|
69 |
function core(x,len){
|
|
|
70 |
x[len>>5]|=0x80<<((len)%32);
|
|
|
71 |
x[(((len+64)>>>9)<<4)+14]=len;
|
|
|
72 |
var a= 1732584193;
|
|
|
73 |
var b=-271733879;
|
|
|
74 |
var c=-1732584194;
|
|
|
75 |
var d= 271733878;
|
|
|
76 |
for(var i=0; i<x.length; i+=16){
|
|
|
77 |
var olda=a;
|
|
|
78 |
var oldb=b;
|
|
|
79 |
var oldc=c;
|
|
|
80 |
var oldd=d;
|
|
|
81 |
|
|
|
82 |
a=FF(a,b,c,d,x[i+ 0],7 ,-680876936);
|
|
|
83 |
d=FF(d,a,b,c,x[i+ 1],12,-389564586);
|
|
|
84 |
c=FF(c,d,a,b,x[i+ 2],17, 606105819);
|
|
|
85 |
b=FF(b,c,d,a,x[i+ 3],22,-1044525330);
|
|
|
86 |
a=FF(a,b,c,d,x[i+ 4],7 ,-176418897);
|
|
|
87 |
d=FF(d,a,b,c,x[i+ 5],12, 1200080426);
|
|
|
88 |
c=FF(c,d,a,b,x[i+ 6],17,-1473231341);
|
|
|
89 |
b=FF(b,c,d,a,x[i+ 7],22,-45705983);
|
|
|
90 |
a=FF(a,b,c,d,x[i+ 8],7 , 1770035416);
|
|
|
91 |
d=FF(d,a,b,c,x[i+ 9],12,-1958414417);
|
|
|
92 |
c=FF(c,d,a,b,x[i+10],17,-42063);
|
|
|
93 |
b=FF(b,c,d,a,x[i+11],22,-1990404162);
|
|
|
94 |
a=FF(a,b,c,d,x[i+12],7 , 1804603682);
|
|
|
95 |
d=FF(d,a,b,c,x[i+13],12,-40341101);
|
|
|
96 |
c=FF(c,d,a,b,x[i+14],17,-1502002290);
|
|
|
97 |
b=FF(b,c,d,a,x[i+15],22, 1236535329);
|
|
|
98 |
|
|
|
99 |
a=GG(a,b,c,d,x[i+ 1],5 ,-165796510);
|
|
|
100 |
d=GG(d,a,b,c,x[i+ 6],9 ,-1069501632);
|
|
|
101 |
c=GG(c,d,a,b,x[i+11],14, 643717713);
|
|
|
102 |
b=GG(b,c,d,a,x[i+ 0],20,-373897302);
|
|
|
103 |
a=GG(a,b,c,d,x[i+ 5],5 ,-701558691);
|
|
|
104 |
d=GG(d,a,b,c,x[i+10],9 , 38016083);
|
|
|
105 |
c=GG(c,d,a,b,x[i+15],14,-660478335);
|
|
|
106 |
b=GG(b,c,d,a,x[i+ 4],20,-405537848);
|
|
|
107 |
a=GG(a,b,c,d,x[i+ 9],5 , 568446438);
|
|
|
108 |
d=GG(d,a,b,c,x[i+14],9 ,-1019803690);
|
|
|
109 |
c=GG(c,d,a,b,x[i+ 3],14,-187363961);
|
|
|
110 |
b=GG(b,c,d,a,x[i+ 8],20, 1163531501);
|
|
|
111 |
a=GG(a,b,c,d,x[i+13],5 ,-1444681467);
|
|
|
112 |
d=GG(d,a,b,c,x[i+ 2],9 ,-51403784);
|
|
|
113 |
c=GG(c,d,a,b,x[i+ 7],14, 1735328473);
|
|
|
114 |
b=GG(b,c,d,a,x[i+12],20,-1926607734);
|
|
|
115 |
|
|
|
116 |
a=HH(a,b,c,d,x[i+ 5],4 ,-378558);
|
|
|
117 |
d=HH(d,a,b,c,x[i+ 8],11,-2022574463);
|
|
|
118 |
c=HH(c,d,a,b,x[i+11],16, 1839030562);
|
|
|
119 |
b=HH(b,c,d,a,x[i+14],23,-35309556);
|
|
|
120 |
a=HH(a,b,c,d,x[i+ 1],4 ,-1530992060);
|
|
|
121 |
d=HH(d,a,b,c,x[i+ 4],11, 1272893353);
|
|
|
122 |
c=HH(c,d,a,b,x[i+ 7],16,-155497632);
|
|
|
123 |
b=HH(b,c,d,a,x[i+10],23,-1094730640);
|
|
|
124 |
a=HH(a,b,c,d,x[i+13],4 , 681279174);
|
|
|
125 |
d=HH(d,a,b,c,x[i+ 0],11,-358537222);
|
|
|
126 |
c=HH(c,d,a,b,x[i+ 3],16,-722521979);
|
|
|
127 |
b=HH(b,c,d,a,x[i+ 6],23, 76029189);
|
|
|
128 |
a=HH(a,b,c,d,x[i+ 9],4 ,-640364487);
|
|
|
129 |
d=HH(d,a,b,c,x[i+12],11,-421815835);
|
|
|
130 |
c=HH(c,d,a,b,x[i+15],16, 530742520);
|
|
|
131 |
b=HH(b,c,d,a,x[i+ 2],23,-995338651);
|
|
|
132 |
|
|
|
133 |
a=II(a,b,c,d,x[i+ 0],6 ,-198630844);
|
|
|
134 |
d=II(d,a,b,c,x[i+ 7],10, 1126891415);
|
|
|
135 |
c=II(c,d,a,b,x[i+14],15,-1416354905);
|
|
|
136 |
b=II(b,c,d,a,x[i+ 5],21,-57434055);
|
|
|
137 |
a=II(a,b,c,d,x[i+12],6 , 1700485571);
|
|
|
138 |
d=II(d,a,b,c,x[i+ 3],10,-1894986606);
|
|
|
139 |
c=II(c,d,a,b,x[i+10],15,-1051523);
|
|
|
140 |
b=II(b,c,d,a,x[i+ 1],21,-2054922799);
|
|
|
141 |
a=II(a,b,c,d,x[i+ 8],6 , 1873313359);
|
|
|
142 |
d=II(d,a,b,c,x[i+15],10,-30611744);
|
|
|
143 |
c=II(c,d,a,b,x[i+ 6],15,-1560198380);
|
|
|
144 |
b=II(b,c,d,a,x[i+13],21, 1309151649);
|
|
|
145 |
a=II(a,b,c,d,x[i+ 4],6 ,-145523070);
|
|
|
146 |
d=II(d,a,b,c,x[i+11],10,-1120210379);
|
|
|
147 |
c=II(c,d,a,b,x[i+ 2],15, 718787259);
|
|
|
148 |
b=II(b,c,d,a,x[i+ 9],21,-343485551);
|
|
|
149 |
|
|
|
150 |
a = add(a,olda);
|
|
|
151 |
b = add(b,oldb);
|
|
|
152 |
c = add(c,oldc);
|
|
|
153 |
d = add(d,oldd);
|
|
|
154 |
}
|
|
|
155 |
return [a,b,c,d];
|
|
|
156 |
}
|
|
|
157 |
function hmac(data,key){
|
|
|
158 |
var wa=toWord(key);
|
|
|
159 |
if(wa.length>16) wa=core(wa,key.length*chrsz);
|
|
|
160 |
var l=[], r=[];
|
|
|
161 |
for(var i=0; i<16; i++){
|
|
|
162 |
l[i]=wa[i]^0x36363636;
|
|
|
163 |
r[i]=wa[i]^0x5c5c5c5c;
|
|
|
164 |
}
|
|
|
165 |
var h=core(l.concat(toWord(data)),512+data.length*chrsz);
|
|
|
166 |
return core(r.concat(h),640);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
// Public functions
|
|
|
170 |
this.compute=function(/* string */data, /* dojox.crypto.outputTypes */outputType){
|
|
|
171 |
// summary
|
|
|
172 |
// computes the digest of data, and returns the result as a string of type outputType
|
|
|
173 |
var out=outputType||dojox.crypto.outputTypes.Base64;
|
|
|
174 |
switch(out){
|
|
|
175 |
case dojox.crypto.outputTypes.Hex:{
|
|
|
176 |
return toHex(core(toWord(data),data.length*chrsz)); // string
|
|
|
177 |
}
|
|
|
178 |
case dojox.crypto.outputTypes.String:{
|
|
|
179 |
return toString(core(toWord(data),data.length*chrsz)); // string
|
|
|
180 |
}
|
|
|
181 |
default:{
|
|
|
182 |
return toBase64(core(toWord(data),data.length*chrsz)); // string
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
};
|
|
|
186 |
this.getHMAC=function(/* string */data, /* string */key, /* dojox.crypto.outputTypes */outputType){
|
|
|
187 |
// summary
|
|
|
188 |
// computes a digest of data using key, and returns the result as a string of outputType
|
|
|
189 |
var out=outputType||dojox.crypto.outputTypes.Base64;
|
|
|
190 |
switch(out){
|
|
|
191 |
case dojox.crypto.outputTypes.Hex:{
|
|
|
192 |
return toHex(hmac(data,key)); // string
|
|
|
193 |
}
|
|
|
194 |
case dojox.crypto.outputTypes.String:{
|
|
|
195 |
return toString(hmac(data,key)); // string
|
|
|
196 |
}
|
|
|
197 |
default:{
|
|
|
198 |
return toBase64(hmac(data,key)); // string
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
};
|
|
|
202 |
}();
|
|
|
203 |
|
|
|
204 |
}
|