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.

30 lines
625 B

4 years ago
  1. 'use strict'
  2. var inherits = require('inherits')
  3. var MD5 = require('md5.js')
  4. var RIPEMD160 = require('ripemd160')
  5. var sha = require('sha.js')
  6. var Base = require('cipher-base')
  7. function Hash (hash) {
  8. Base.call(this, 'digest')
  9. this._hash = hash
  10. }
  11. inherits(Hash, Base)
  12. Hash.prototype._update = function (data) {
  13. this._hash.update(data)
  14. }
  15. Hash.prototype._final = function () {
  16. return this._hash.digest()
  17. }
  18. module.exports = function createHash (alg) {
  19. alg = alg.toLowerCase()
  20. if (alg === 'md5') return new MD5()
  21. if (alg === 'rmd160' || alg === 'ripemd160') return new RIPEMD160()
  22. return new Hash(sha(alg))
  23. }