Sha256: a64a607dcb642323a9fcd9e7ab3fe189bce6f1a658352fd6fcba84af997f7ee8
Contents?: true
Size: 1017 Bytes
Versions: 65
Compression:
Stored size: 1017 Bytes
Contents
var createHash = require('./create-hash') var zeroBuffer = new Buffer(128) zeroBuffer.fill(0) module.exports = Hmac function Hmac (alg, key) { if(!(this instanceof Hmac)) return new Hmac(alg, key) this._opad = opad this._alg = alg var blocksize = (alg === 'sha512') ? 128 : 64 key = this._key = !Buffer.isBuffer(key) ? new Buffer(key) : key if(key.length > blocksize) { key = createHash(alg).update(key).digest() } else if(key.length < blocksize) { key = Buffer.concat([key, zeroBuffer], blocksize) } var ipad = this._ipad = new Buffer(blocksize) var opad = this._opad = new Buffer(blocksize) for(var i = 0; i < blocksize; i++) { ipad[i] = key[i] ^ 0x36 opad[i] = key[i] ^ 0x5C } this._hash = createHash(alg).update(ipad) } Hmac.prototype.update = function (data, enc) { this._hash.update(data, enc) return this } Hmac.prototype.digest = function (enc) { var h = this._hash.digest() return createHash(this._alg).update(this._opad).update(h).digest(enc) }
Version data entries
65 entries across 65 versions & 2 rubygems