react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / create-hmac / browser.js
80542 views'use strict';1var createHash = require('create-hash/browser');2var inherits = require('inherits')34var Transform = require('stream').Transform56var ZEROS = new Buffer(128)7ZEROS.fill(0)89function Hmac(alg, key) {10Transform.call(this)1112if (typeof key === 'string') {13key = new Buffer(key)14}1516var blocksize = (alg === 'sha512' || alg === 'sha384') ? 128 : 641718this._alg = alg19this._key = key2021if (key.length > blocksize) {22key = createHash(alg).update(key).digest()2324} else if (key.length < blocksize) {25key = Buffer.concat([key, ZEROS], blocksize)26}2728var ipad = this._ipad = new Buffer(blocksize)29var opad = this._opad = new Buffer(blocksize)3031for (var i = 0; i < blocksize; i++) {32ipad[i] = key[i] ^ 0x3633opad[i] = key[i] ^ 0x5C34}3536this._hash = createHash(alg).update(ipad)37}3839inherits(Hmac, Transform)4041Hmac.prototype.update = function (data, enc) {42this._hash.update(data, enc)4344return this45}4647Hmac.prototype._transform = function (data, _, next) {48this._hash.update(data)4950next()51}5253Hmac.prototype._flush = function (next) {54this.push(this.digest())5556next()57}5859Hmac.prototype.digest = function (enc) {60var h = this._hash.digest()6162return createHash(this._alg).update(this._opad).update(h).digest(enc)63}6465module.exports = function createHmac(alg, key) {66return new Hmac(alg, key)67}686970