| 1 |
define([], function () { |
| 2 |
|
| 3 |
|
| 4 |
return function (_sha, _keyLength, _blockSize, _hash) { |
| 5 |
function hasher(data, 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 (data, key, outputType) { |
| 26 |
outputType = outputType || _sha.outputTypes.Base64; |
| 27 |
|
| 28 |
|
| 29 |
var wa = _sha.toWord(key); |
| 30 |
if(wa.length > 16){ |
| 31 |
wa = _sha.digest(wa, key.length * 8, _hash, _keyLength); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 65 |
hasher._hmac = hasher.hmac; |
| 66 |
|
| 67 |
return hasher; |
| 68 |
}; |
| 69 |
}); |