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