root / trunk / web / dojo / dojox / encoding / digests / SHA1.js
History | View | Annotate | Download (2.91 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.encoding.digests.SHA1"]){ |
||
9 | dojo._hasResource["dojox.encoding.digests.SHA1"]=true; |
||
10 | dojo.provide("dojox.encoding.digests.SHA1");
|
||
11 | dojo.require("dojox.encoding.digests._base");
|
||
12 | (function(){
|
||
13 | var _1=dojox.encoding.digests;
|
||
14 | var _2=8,_3=(1<<_2)-1; |
||
15 | function R(n,c){ |
||
16 | return (n<<c)|(n>>>(32-c)); |
||
17 | }; |
||
18 | function FT(t,b,c,d){ |
||
19 | if(t<20){ |
||
20 | return (b&c)|((~b)&d);
|
||
21 | } |
||
22 | if(t<40){ |
||
23 | return b^c^d;
|
||
24 | } |
||
25 | if(t<60){ |
||
26 | return (b&c)|(b&d)|(c&d);
|
||
27 | } |
||
28 | return b^c^d;
|
||
29 | }; |
||
30 | function KT(t){ |
||
31 | return (t<20)?1518500249:(t<40)?1859775393:(t<60)?-1894007588:-899497514; |
||
32 | }; |
||
33 | function _4(x,_5){ |
||
34 | x[_5>>5]|=128<<(24-_5%32); |
||
35 | x[((_5+64>>9)<<4)+15]=_5; |
||
36 | var w=new Array(80),a=1732584193,b=-271733879,c=-1732584194,d=271733878,e=-1009589776; |
||
37 | for(var i=0;i<x.length;i+=16){ |
||
38 | var _6=a,_7=b,_8=c,_9=d,_a=e;
|
||
39 | for(var j=0;j<80;j++){ |
||
40 | if(j<16){ |
||
41 | w[j]=x[i+j]; |
||
42 | }else{
|
||
43 | w[j]=R(w[j-3]^w[j-8]^w[j-14]^w[j-16],1); |
||
44 | } |
||
45 | var t=_1.addWords(_1.addWords(R(a,5),FT(j,b,c,d)),_1.addWords(_1.addWords(e,w[j]),KT(j))); |
||
46 | e=d; |
||
47 | d=c; |
||
48 | c=R(b,30);
|
||
49 | b=a; |
||
50 | a=t; |
||
51 | } |
||
52 | a=_1.addWords(a,_6); |
||
53 | b=_1.addWords(b,_7); |
||
54 | c=_1.addWords(c,_8); |
||
55 | d=_1.addWords(d,_9); |
||
56 | e=_1.addWords(e,_a); |
||
57 | } |
||
58 | return [a,b,c,d,e];
|
||
59 | }; |
||
60 | function _b(_c,_d){ |
||
61 | var wa=_e(_d);
|
||
62 | if(wa.length>16){ |
||
63 | wa=_4(wa,_d.length*_2); |
||
64 | } |
||
65 | var _f=new Array(16),_10=new Array(16); |
||
66 | for(var i=0;i<16;i++){ |
||
67 | _f[i]=wa[i]^909522486;
|
||
68 | _10[i]=wa[i]^1549556828;
|
||
69 | } |
||
70 | var _11=_4(_f.concat(_e(_c)),512+_c.length*_2); |
||
71 | return _4(_10.concat(_11),512+160); |
||
72 | }; |
||
73 | function _e(s){ |
||
74 | var wa=[];
|
||
75 | for(var i=0,l=s.length*_2;i<l;i+=_2){ |
||
76 | wa[i>>5]|=(s.charCodeAt(i/_2)&_3)<<(32-_2-i%32); |
||
77 | } |
||
78 | return wa;
|
||
79 | }; |
||
80 | function _12(wa){ |
||
81 | var h="0123456789abcdef",s=[]; |
||
82 | for(var i=0,l=wa.length*4;i<l;i++){ |
||
83 | s.push(h.charAt((wa[i>>2]>>((3-i%4)*8+4))&15),h.charAt((wa[i>>2]>>((3-i%4)*8))&15)); |
||
84 | } |
||
85 | return s.join(""); |
||
86 | }; |
||
87 | function _13(wa){ |
||
88 | var s=[];
|
||
89 | for(var i=0,l=wa.length*32;i<l;i+=_2){ |
||
90 | s.push(String.fromCharCode((wa[i>>5]>>>(32-_2-i%32))&_3)); |
||
91 | } |
||
92 | return s.join(""); |
||
93 | }; |
||
94 | function _14(wa){ |
||
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))&255)<<16)|(((wa[i+1>>2]>>8*(3-(i+1)%4))&255)<<8)|((wa[i+2>>2]>>8*(3-(i+2)%4))&255); |
||
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))&63)); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | return s.join(""); |
||
107 | }; |
||
108 | _1.SHA1=function(_15,_16){ |
||
109 | var out=_16||_1.outputTypes.Base64;
|
||
110 | var wa=_4(_e(_15),_15.length*_2);
|
||
111 | switch(out){
|
||
112 | case _1.outputTypes.Raw:
|
||
113 | return wa;
|
||
114 | case _1.outputTypes.Hex:
|
||
115 | return _12(wa);
|
||
116 | case _1.outputTypes.String:
|
||
117 | return _13(wa);
|
||
118 | default:
|
||
119 | return _14(wa);
|
||
120 | } |
||
121 | }; |
||
122 | _1.SHA1._hmac=function(_17,key,_18){ |
||
123 | var out=_18||_1.outputTypes.Base64;
|
||
124 | var wa=_b(_17,key);
|
||
125 | switch(out){
|
||
126 | case _1.outputTypes.Raw:
|
||
127 | return wa;
|
||
128 | case _1.outputTypes.Hex:
|
||
129 | return _12(wa);
|
||
130 | case _1.outputTypes.String:
|
||
131 | return _13(wa);
|
||
132 | default:
|
||
133 | return _14(wa);
|
||
134 | } |
||
135 | }; |
||
136 | })(); |
||
137 | } |