You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
804 B
39 lines
804 B
|
2 months ago
|
import Crypto from './crypto.js'
|
||
|
|
|
||
|
|
(function() {
|
||
|
|
|
||
|
|
// Shortcut
|
||
|
|
var util = Crypto.util;
|
||
|
|
|
||
|
|
Crypto.HMAC = function(hasher, message, key, options) {
|
||
|
|
|
||
|
|
// Allow arbitrary length keys
|
||
|
|
key = key.length > hasher._blocksize * 4 ?
|
||
|
|
hasher(key, {
|
||
|
|
asBytes: true
|
||
|
|
}) :
|
||
|
|
util.stringToBytes(key);
|
||
|
|
|
||
|
|
// XOR keys with pad constants
|
||
|
|
var okey = key,
|
||
|
|
ikey = key.slice(0);
|
||
|
|
for (var i = 0; i < hasher._blocksize * 4; i++) {
|
||
|
|
okey[i] ^= 0x5C;
|
||
|
|
ikey[i] ^= 0x36;
|
||
|
|
}
|
||
|
|
|
||
|
|
var hmacbytes = hasher(util.bytesToString(okey) +
|
||
|
|
hasher(util.bytesToString(ikey) + message, {
|
||
|
|
asString: true
|
||
|
|
}), {
|
||
|
|
asBytes: true
|
||
|
|
});
|
||
|
|
return options && options.asBytes ? hmacbytes :
|
||
|
|
options && options.asString ? util.bytesToString(hmacbytes) :
|
||
|
|
util.bytesToHex(hmacbytes);
|
||
|
|
|
||
|
|
};
|
||
|
|
})();
|
||
|
|
|
||
|
|
module.exports = Crypto;
|