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.
37 lines
1.0 KiB
37 lines
1.0 KiB
2 years ago
|
import { inherits as inherits$0 } from "util";
|
||
|
import Base from "../cipher-base";
|
||
|
'use strict';
|
||
|
var inherits = { inherits: inherits$0 }.inherits;
|
||
|
var ZEROS = Buffer.alloc(128);
|
||
|
var blocksize = 64;
|
||
|
function Hmac(alg, key) {
|
||
|
Base.call(this, 'digest');
|
||
|
if (typeof key === 'string') {
|
||
|
key = Buffer.from(key);
|
||
|
}
|
||
|
this._alg = alg;
|
||
|
this._key = key;
|
||
|
if (key.length > blocksize) {
|
||
|
key = alg(key);
|
||
|
}
|
||
|
else if (key.length < blocksize) {
|
||
|
key = Buffer.concat([key, ZEROS], blocksize);
|
||
|
}
|
||
|
var ipad = this._ipad = Buffer.allocUnsafe(blocksize);
|
||
|
var opad = this._opad = Buffer.allocUnsafe(blocksize);
|
||
|
for (var i = 0; i < blocksize; i++) {
|
||
|
ipad[i] = key[i] ^ 0x36;
|
||
|
opad[i] = key[i] ^ 0x5C;
|
||
|
}
|
||
|
this._hash = [ipad];
|
||
|
}
|
||
|
inherits(Hmac, Base);
|
||
|
Hmac.prototype._update = function (data) {
|
||
|
this._hash.push(data);
|
||
|
};
|
||
|
Hmac.prototype._final = function () {
|
||
|
var h = this._alg(Buffer.concat(this._hash));
|
||
|
return this._alg(Buffer.concat([this._opad, h]));
|
||
|
};
|
||
|
export default Hmac;
|