]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/encoding/digests/MD5.js
eb72d59262e47444c47f1ec26162d3b0519b1005
[eow] / static / dojo-release-1.1.1 / dojox / encoding / digests / MD5.js
1 if(!dojo._hasResource["dojox.encoding.digests.MD5"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.encoding.digests.MD5"] = true;
3 dojo.provide("dojox.encoding.digests.MD5");
4
5 dojo.require("dojox.encoding.digests._base");
6
7 /*      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 (function(){
17         var dxd=dojox.encoding.digests;
18         var chrsz=8;
19
20         //      MD5 rounds functions
21         function R(n,c){ return (n<<c)|(n>>>(32-c)); }
22         function C(q,a,b,x,s,t){ return dxd.addWords(R(dxd.addWords(dxd.addWords(a, q), dxd.addWords(x, t)), s), b); }
23         function FF(a,b,c,d,x,s,t){ return C((b&c)|((~b)&d),a,b,x,s,t); }
24         function GG(a,b,c,d,x,s,t){ return C((b&d)|(c&(~d)),a,b,x,s,t); }
25         function HH(a,b,c,d,x,s,t){ return C(b^c^d,a,b,x,s,t); }
26         function II(a,b,c,d,x,s,t){ return C(c^(b|(~d)),a,b,x,s,t); }
27
28         //      the core MD5 rounds method
29         function core(x,len){
30                 x[len>>5]|=0x80<<((len)%32);
31                 x[(((len+64)>>>9)<<4)+14]=len;
32                 var a= 1732584193;
33                 var b=-271733879;
34                 var c=-1732584194;
35                 var d= 271733878;
36                 for(var i=0; i<x.length; i+=16){
37                         var olda=a;
38                         var oldb=b;
39                         var oldc=c;
40                         var oldd=d;
41
42                         a=FF(a,b,c,d,x[i+ 0],7 ,-680876936);
43                         d=FF(d,a,b,c,x[i+ 1],12,-389564586);
44                         c=FF(c,d,a,b,x[i+ 2],17, 606105819);
45                         b=FF(b,c,d,a,x[i+ 3],22,-1044525330);
46                         a=FF(a,b,c,d,x[i+ 4],7 ,-176418897);
47                         d=FF(d,a,b,c,x[i+ 5],12, 1200080426);
48                         c=FF(c,d,a,b,x[i+ 6],17,-1473231341);
49                         b=FF(b,c,d,a,x[i+ 7],22,-45705983);
50                         a=FF(a,b,c,d,x[i+ 8],7 , 1770035416);
51                         d=FF(d,a,b,c,x[i+ 9],12,-1958414417);
52                         c=FF(c,d,a,b,x[i+10],17,-42063);
53                         b=FF(b,c,d,a,x[i+11],22,-1990404162);
54                         a=FF(a,b,c,d,x[i+12],7 , 1804603682);
55                         d=FF(d,a,b,c,x[i+13],12,-40341101);
56                         c=FF(c,d,a,b,x[i+14],17,-1502002290);
57                         b=FF(b,c,d,a,x[i+15],22, 1236535329);
58
59                         a=GG(a,b,c,d,x[i+ 1],5 ,-165796510);
60                         d=GG(d,a,b,c,x[i+ 6],9 ,-1069501632);
61                         c=GG(c,d,a,b,x[i+11],14, 643717713);
62                         b=GG(b,c,d,a,x[i+ 0],20,-373897302);
63                         a=GG(a,b,c,d,x[i+ 5],5 ,-701558691);
64                         d=GG(d,a,b,c,x[i+10],9 , 38016083);
65                         c=GG(c,d,a,b,x[i+15],14,-660478335);
66                         b=GG(b,c,d,a,x[i+ 4],20,-405537848);
67                         a=GG(a,b,c,d,x[i+ 9],5 , 568446438);
68                         d=GG(d,a,b,c,x[i+14],9 ,-1019803690);
69                         c=GG(c,d,a,b,x[i+ 3],14,-187363961);
70                         b=GG(b,c,d,a,x[i+ 8],20, 1163531501);
71                         a=GG(a,b,c,d,x[i+13],5 ,-1444681467);
72                         d=GG(d,a,b,c,x[i+ 2],9 ,-51403784);
73                         c=GG(c,d,a,b,x[i+ 7],14, 1735328473);
74                         b=GG(b,c,d,a,x[i+12],20,-1926607734);
75
76                         a=HH(a,b,c,d,x[i+ 5],4 ,-378558);
77                         d=HH(d,a,b,c,x[i+ 8],11,-2022574463);
78                         c=HH(c,d,a,b,x[i+11],16, 1839030562);
79                         b=HH(b,c,d,a,x[i+14],23,-35309556);
80                         a=HH(a,b,c,d,x[i+ 1],4 ,-1530992060);
81                         d=HH(d,a,b,c,x[i+ 4],11, 1272893353);
82                         c=HH(c,d,a,b,x[i+ 7],16,-155497632);
83                         b=HH(b,c,d,a,x[i+10],23,-1094730640);
84                         a=HH(a,b,c,d,x[i+13],4 , 681279174);
85                         d=HH(d,a,b,c,x[i+ 0],11,-358537222);
86                         c=HH(c,d,a,b,x[i+ 3],16,-722521979);
87                         b=HH(b,c,d,a,x[i+ 6],23, 76029189);
88                         a=HH(a,b,c,d,x[i+ 9],4 ,-640364487);
89                         d=HH(d,a,b,c,x[i+12],11,-421815835);
90                         c=HH(c,d,a,b,x[i+15],16, 530742520);
91                         b=HH(b,c,d,a,x[i+ 2],23,-995338651);
92
93                         a=II(a,b,c,d,x[i+ 0],6 ,-198630844);
94                         d=II(d,a,b,c,x[i+ 7],10, 1126891415);
95                         c=II(c,d,a,b,x[i+14],15,-1416354905);
96                         b=II(b,c,d,a,x[i+ 5],21,-57434055);
97                         a=II(a,b,c,d,x[i+12],6 , 1700485571);
98                         d=II(d,a,b,c,x[i+ 3],10,-1894986606);
99                         c=II(c,d,a,b,x[i+10],15,-1051523);
100                         b=II(b,c,d,a,x[i+ 1],21,-2054922799);
101                         a=II(a,b,c,d,x[i+ 8],6 , 1873313359);
102                         d=II(d,a,b,c,x[i+15],10,-30611744);
103                         c=II(c,d,a,b,x[i+ 6],15,-1560198380);
104                         b=II(b,c,d,a,x[i+13],21, 1309151649);
105                         a=II(a,b,c,d,x[i+ 4],6 ,-145523070);
106                         d=II(d,a,b,c,x[i+11],10,-1120210379);
107                         c=II(c,d,a,b,x[i+ 2],15, 718787259);
108                         b=II(b,c,d,a,x[i+ 9],21,-343485551);
109
110                         a=dxd.addWords(a, olda);
111                         b=dxd.addWords(b, oldb);
112                         c=dxd.addWords(c, oldc);
113                         d=dxd.addWords(d, oldd);
114                 }
115                 return [a,b,c,d];
116         }
117
118         function hmac(data, key){
119                 var wa=dxd.stringToWord(key);
120                 if(wa.length>16){ 
121                         wa=core(wa, key.length*chrsz);
122                 }
123                 var l=[], r=[];
124                 for(var i=0; i<16; i++){
125                         l[i]=wa[i]^0x36363636;
126                         r[i]=wa[i]^0x5c5c5c5c;
127                 }
128                 var h=core(l.concat(dxd.stringToWord(data)), 512+data.length*chrsz);
129                 return core(r.concat(h), 640);
130         }
131
132         //      public function
133         dxd.MD5=function(/* string */data, /* dojox.encoding.digests.outputTypes? */outputType){
134                 //      summary
135                 //      computes the digest of data, and returns the result according to type outputType
136                 var out=outputType || dxd.outputTypes.Base64;
137                 var wa=core(dxd.stringToWord(data), data.length*chrsz);
138                 switch(out){
139                         case dxd.outputTypes.Raw:{
140                                 return wa;      //      word[]
141                         }
142                         case dxd.outputTypes.Hex:{
143                                 return dxd.wordToHex(wa);       //      string
144                         }
145                         case dxd.outputTypes.String:{
146                                 return dxd.wordToString(wa);    //      string
147                         }
148                         default:{
149                                 return dxd.wordToBase64(wa);    //      string
150                         }
151                 }
152         };
153
154         //      make this private, for later use with a generic HMAC calculator.
155         dxd.MD5._hmac=function(/* string */data, /* string */key, /* dojox.encoding.digests.outputTypes? */outputType){
156                 //      summary
157                 //      computes the digest of data, and returns the result according to type outputType
158                 var out=outputType || dxd.outputTypes.Base64;
159                 var wa=hmac(data, key);
160                 switch(out){
161                         case dxd.outputTypes.Raw:{
162                                 return wa;      //      word[]
163                         }
164                         case dxd.outputTypes.Hex:{
165                                 return dxd.wordToHex(wa);       //      string
166                         }
167                         case dxd.outputTypes.String:{
168                                 return dxd.wordToString(wa);    //      string
169                         }
170                         default:{
171                                 return dxd.wordToBase64(wa);    //      string
172                         }
173                 }
174         };
175 })();
176
177 }