browser.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict'
  2. var inherits = require('inherits')
  3. var Legacy = require('./legacy')
  4. var Base = require('cipher-base')
  5. var Buffer = require('safe-buffer').Buffer
  6. var md5 = require('create-hash/md5')
  7. var RIPEMD160 = require('ripemd160')
  8. var sha = require('sha.js')
  9. var ZEROS = Buffer.alloc(128)
  10. function Hmac (alg, key) {
  11. Base.call(this, 'digest')
  12. if (typeof key === 'string') {
  13. key = Buffer.from(key)
  14. }
  15. var blocksize = (alg === 'sha512' || alg === 'sha384') ? 128 : 64
  16. this._alg = alg
  17. this._key = key
  18. if (key.length > blocksize) {
  19. var hash = alg === 'rmd160' ? new RIPEMD160() : sha(alg)
  20. key = hash.update(key).digest()
  21. } else if (key.length < blocksize) {
  22. key = Buffer.concat([key, ZEROS], blocksize)
  23. }
  24. var ipad = this._ipad = Buffer.allocUnsafe(blocksize)
  25. var opad = this._opad = Buffer.allocUnsafe(blocksize)
  26. for (var i = 0; i < blocksize; i++) {
  27. ipad[i] = key[i] ^ 0x36
  28. opad[i] = key[i] ^ 0x5C
  29. }
  30. this._hash = alg === 'rmd160' ? new RIPEMD160() : sha(alg)
  31. this._hash.update(ipad)
  32. }
  33. inherits(Hmac, Base)
  34. Hmac.prototype._update = function (data) {
  35. this._hash.update(data)
  36. }
  37. Hmac.prototype._final = function () {
  38. var h = this._hash.digest()
  39. var hash = this._alg === 'rmd160' ? new RIPEMD160() : sha(this._alg)
  40. return hash.update(this._opad).update(h).digest()
  41. }
  42. module.exports = function createHmac (alg, key) {
  43. alg = alg.toLowerCase()
  44. if (alg === 'rmd160' || alg === 'ripemd160') {
  45. return new Hmac('rmd160', key)
  46. }
  47. if (alg === 'md5') {
  48. return new Legacy(md5, key)
  49. }
  50. return new Hmac(alg, key)
  51. }