Debian Codegraph @

dojox/encoding/digests/_sha2.js

raw symbols SCIP
1 define([], function () {
2 // Return a hashing function for a _sha helper (_sha-32 or _sha-64), a key length, a block size, and a set
3 // of constants.
4 return function (/* sha32/64 */_sha, /* Number */_keyLength, /* Blocksize */_blockSize, /* Array */_hash) {
5 function hasher(/* String */data, /* sha32/64.outputTypes */outputType) {
6 outputType = outputType || _sha.outputTypes.Base64;
7 var wa = _sha.digest(_sha.toWord(data), data.length * 8, _hash, _keyLength);
8
9 switch(outputType){
10 case _sha.outputTypes.Raw: {
11 return wa;
12 }
13 case _sha.outputTypes.Hex: {
14 return _sha.toHex(wa);
15 }
16 case _sha.outputTypes.String: {
17 return _sha._toString(wa);
18 }
19 default: {
20 return _sha.toBase64(wa);
21 }
22 }
23 }
24
25 hasher.hmac = function (/* String */data, /* String */key, /* sha32/64.outputTypes? */outputType) {
26 outputType = outputType || _sha.outputTypes.Base64;
27
28 // prepare the key
29 var wa = _sha.toWord(key);
30 if(wa.length > 16){
31 wa = _sha.digest(wa, key.length * 8, _hash, _keyLength);
32 }
33
34 // set up the pads
35 var numWords = _blockSize / 32,
36 ipad = new Array(numWords),
37 opad = new Array(numWords);
38 for(var i=0; i<numWords; i++){
39 ipad[i] = wa[i] ^ 0x36363636;
40 opad[i] = wa[i] ^ 0x5c5c5c5c;
41 }
42
43 // make the final digest
44 var r1 = _sha.digest(ipad.concat(_sha.toWord(data)), _blockSize + data.length * 8, _hash, _keyLength);
45 var r2 = _sha.digest(opad.concat(r1), _blockSize + _keyLength, _hash, _keyLength);
46
47 // return the output
48 switch(outputType){
49 case _sha.outputTypes.Raw: {
50 return r2;
51 }
52 case _sha.outputTypes.Hex: {
53 return _sha.toHex(r2);
54 }
55 case _sha.outputTypes.String: {
56 return _sha._toString(r2);
57 }
58 default: {
59 return _sha.toBase64(r2);
60 }
61 }
62 };
63
64 // for backwards compatibility
65 hasher._hmac = hasher.hmac;
66
67 return hasher;
68 };
69 });