1 | define(["./_base"], function(dxd) { |
---|
2 | |
---|
3 | /*===== |
---|
4 | dxd = dojox.encoding.digests; |
---|
5 | =====*/ |
---|
6 | |
---|
7 | /* |
---|
8 | * A port of Paul Johnstone's SHA1 implementation |
---|
9 | * |
---|
10 | * Version 2.1a Copyright Paul Johnston 2000 - 2002. |
---|
11 | * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet |
---|
12 | * Distributed under the BSD License |
---|
13 | * See http://pajhome.org.uk/crypt/md5 for details. |
---|
14 | * |
---|
15 | * Dojo port by Tom Trenka |
---|
16 | */ |
---|
17 | |
---|
18 | var chrsz=8, // change to 16 for unicode. |
---|
19 | mask=(1<<chrsz)-1; |
---|
20 | |
---|
21 | function R(n,c){ return (n<<c)|(n>>>(32-c)); } |
---|
22 | function FT(t,b,c,d){ |
---|
23 | if(t<20){ return (b&c)|((~b)&d); } |
---|
24 | if(t<40){ return b^c^d; } |
---|
25 | if(t<60){ return (b&c)|(b&d)|(c&d); } |
---|
26 | return b^c^d; |
---|
27 | } |
---|
28 | function KT(t){ return (t<20)?1518500249:(t<40)?1859775393:(t<60)?-1894007588:-899497514; } |
---|
29 | |
---|
30 | function core(x,len){ |
---|
31 | x[len>>5]|=0x80<<(24-len%32); |
---|
32 | x[((len+64>>9)<<4)+15]=len; |
---|
33 | |
---|
34 | var w=new Array(80), a=1732584193, b=-271733879, c=-1732584194, d=271733878, e=-1009589776; |
---|
35 | for(var i=0; i<x.length; i+=16){ |
---|
36 | var olda=a, oldb=b, oldc=c, oldd=d, olde=e; |
---|
37 | for(var j=0;j<80;j++){ |
---|
38 | if(j<16){ w[j]=x[i+j]; } |
---|
39 | else { w[j]=R(w[j-3]^w[j-8]^w[j-14]^w[j-16],1); } |
---|
40 | var t = dxd.addWords(dxd.addWords(R(a,5),FT(j,b,c,d)),dxd.addWords(dxd.addWords(e,w[j]),KT(j))); |
---|
41 | e=d; d=c; c=R(b,30); b=a; a=t; |
---|
42 | } |
---|
43 | a=dxd.addWords(a,olda); |
---|
44 | b=dxd.addWords(b,oldb); |
---|
45 | c=dxd.addWords(c,oldc); |
---|
46 | d=dxd.addWords(d,oldd); |
---|
47 | e=dxd.addWords(e,olde); |
---|
48 | } |
---|
49 | return [a, b, c, d, e]; |
---|
50 | } |
---|
51 | |
---|
52 | function hmac(data, key){ |
---|
53 | var wa=toWord(key); |
---|
54 | if(wa.length>16){ wa=core(wa, key.length*chrsz); } |
---|
55 | |
---|
56 | var ipad=new Array(16), opad=new Array(16); |
---|
57 | for(var i=0;i<16;i++){ |
---|
58 | ipad[i]=wa[i]^0x36363636; |
---|
59 | opad[i]=wa[i]^0x5c5c5c5c; |
---|
60 | } |
---|
61 | |
---|
62 | var hash=core(ipad.concat(toWord(data)),512+data.length*chrsz); |
---|
63 | return core(opad.concat(hash), 512+160); |
---|
64 | } |
---|
65 | |
---|
66 | function toWord(s){ |
---|
67 | var wa=[]; |
---|
68 | for(var i=0, l=s.length*chrsz; i<l; i+=chrsz){ |
---|
69 | wa[i>>5]|=(s.charCodeAt(i/chrsz)&mask)<<(32-chrsz-i%32); |
---|
70 | } |
---|
71 | return wa; // word[] |
---|
72 | } |
---|
73 | |
---|
74 | function toHex(wa){ |
---|
75 | // slightly different than the common one. |
---|
76 | var h="0123456789abcdef", s=[]; |
---|
77 | for(var i=0, l=wa.length*4; i<l; i++){ |
---|
78 | s.push(h.charAt((wa[i>>2]>>((3-i%4)*8+4))&0xF), h.charAt((wa[i>>2]>>((3-i%4)*8))&0xF)); |
---|
79 | } |
---|
80 | return s.join(""); // string |
---|
81 | } |
---|
82 | |
---|
83 | function _toString(wa){ |
---|
84 | var s=[]; |
---|
85 | for(var i=0, l=wa.length*32; i<l; i+=chrsz){ |
---|
86 | s.push(String.fromCharCode((wa[i>>5]>>>(32-chrsz-i%32))&mask)); |
---|
87 | } |
---|
88 | return s.join(""); // string |
---|
89 | } |
---|
90 | |
---|
91 | function toBase64(/* word[] */wa){ |
---|
92 | // summary: |
---|
93 | // convert an array of words to base64 encoding, should be more efficient |
---|
94 | // than using dojox.encoding.base64 |
---|
95 | var p="=", tab="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", s=[]; |
---|
96 | for(var i=0, l=wa.length*4; i<l; i+=3){ |
---|
97 | var t=(((wa[i>>2]>>8*(3-i%4))&0xFF)<<16)|(((wa[i+1>>2]>>8*(3-(i+1)%4))&0xFF)<<8)|((wa[i+2>>2]>>8*(3-(i+2)%4))&0xFF); |
---|
98 | for(var j=0; j<4; j++){ |
---|
99 | if(i*8+j*6>wa.length*32){ |
---|
100 | s.push(p); |
---|
101 | } else { |
---|
102 | s.push(tab.charAt((t>>6*(3-j))&0x3F)); |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | return s.join(""); // string |
---|
107 | }; |
---|
108 | |
---|
109 | // public function |
---|
110 | dxd.SHA1=function(/* String */data, /* dojox.encoding.digests.outputTypes? */outputType){ |
---|
111 | // summary: |
---|
112 | // Computes the SHA1 digest of the data, and returns the result according to output type. |
---|
113 | var out=outputType||dxd.outputTypes.Base64; |
---|
114 | var wa=core(toWord(data), data.length*chrsz); |
---|
115 | switch(out){ |
---|
116 | case dxd.outputTypes.Raw:{ |
---|
117 | return wa; // word[] |
---|
118 | } |
---|
119 | case dxd.outputTypes.Hex:{ |
---|
120 | return toHex(wa); // string |
---|
121 | } |
---|
122 | case dxd.outputTypes.String:{ |
---|
123 | return _toString(wa); // string |
---|
124 | } |
---|
125 | default:{ |
---|
126 | return toBase64(wa); // string |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | // make this private, for later use with a generic HMAC calculator. |
---|
132 | dxd.SHA1._hmac=function(/* string */data, /* string */key, /* dojox.encoding.digests.outputTypes? */outputType){ |
---|
133 | // summary: |
---|
134 | // computes the digest of data, and returns the result according to type outputType |
---|
135 | var out=outputType || dxd.outputTypes.Base64; |
---|
136 | var wa=hmac(data, key); |
---|
137 | switch(out){ |
---|
138 | case dxd.outputTypes.Raw:{ |
---|
139 | return wa; // word[] |
---|
140 | } |
---|
141 | case dxd.outputTypes.Hex:{ |
---|
142 | return toHex(wa); // string |
---|
143 | } |
---|
144 | case dxd.outputTypes.String:{ |
---|
145 | return _toString(wa); // string |
---|
146 | } |
---|
147 | default:{ |
---|
148 | return toBase64(wa); // string |
---|
149 | } |
---|
150 | } |
---|
151 | }; |
---|
152 | |
---|
153 | return dxd.SHA1; |
---|
154 | }); |
---|