browser.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict'
  2. var inherits = require('inherits')
  3. var md5 = require('./md5')
  4. var RIPEMD160 = require('ripemd160')
  5. var sha = require('sha.js')
  6. var Base = require('cipher-base')
  7. function HashNoConstructor (hash) {
  8. Base.call(this, 'digest')
  9. this._hash = hash
  10. this.buffers = []
  11. }
  12. inherits(HashNoConstructor, Base)
  13. HashNoConstructor.prototype._update = function (data) {
  14. this.buffers.push(data)
  15. }
  16. HashNoConstructor.prototype._final = function () {
  17. var buf = Buffer.concat(this.buffers)
  18. var r = this._hash(buf)
  19. this.buffers = null
  20. return r
  21. }
  22. function Hash (hash) {
  23. Base.call(this, 'digest')
  24. this._hash = hash
  25. }
  26. inherits(Hash, Base)
  27. Hash.prototype._update = function (data) {
  28. this._hash.update(data)
  29. }
  30. Hash.prototype._final = function () {
  31. return this._hash.digest()
  32. }
  33. module.exports = function createHash (alg) {
  34. alg = alg.toLowerCase()
  35. if (alg === 'md5') return new HashNoConstructor(md5)
  36. if (alg === 'rmd160' || alg === 'ripemd160') return new Hash(new RIPEMD160())
  37. return new Hash(sha(alg))
  38. }