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.

48 lines
1.7 KiB

4 years ago
  1. # hash-base
  2. [![NPM Package](https://img.shields.io/npm/v/hash-base.svg?style=flat-square)](https://www.npmjs.org/package/hash-base)
  3. [![Build Status](https://img.shields.io/travis/crypto-browserify/hash-base.svg?branch=master&style=flat-square)](https://travis-ci.org/crypto-browserify/hash-base)
  4. [![Dependency status](https://img.shields.io/david/crypto-browserify/hash-base.svg?style=flat-square)](https://david-dm.org/crypto-browserify/hash-base#info=dependencies)
  5. [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
  6. Abstract base class to inherit from if you want to create streams implementing the same API as node crypto [Hash][1] (for [Cipher][2] / [Decipher][3] check [crypto-browserify/cipher-base][4]).
  7. ## Example
  8. ```js
  9. const HashBase = require('hash-base')
  10. const inherits = require('inherits')
  11. // our hash function is XOR sum of all bytes
  12. function MyHash () {
  13. HashBase.call(this, 1) // in bytes
  14. this._sum = 0x00
  15. }
  16. inherits(MyHash, HashBase)
  17. MyHash.prototype._update = function () {
  18. for (let i = 0; i < this._block.length; ++i) this._sum ^= this._block[i]
  19. }
  20. MyHash.prototype._digest = function () {
  21. return this._sum
  22. }
  23. const data = Buffer.from([ 0x00, 0x42, 0x01 ])
  24. const hash = new MyHash().update(data).digest()
  25. console.log(hash) // => 67
  26. ```
  27. You also can check [source code](index.js) or [crypto-browserify/md5.js][5]
  28. ## LICENSE
  29. MIT
  30. [1]: https://nodejs.org/api/crypto.html#crypto_class_hash
  31. [2]: https://nodejs.org/api/crypto.html#crypto_class_cipher
  32. [3]: https://nodejs.org/api/crypto.html#crypto_class_decipher
  33. [4]: https://github.com/crypto-browserify/cipher-base
  34. [5]: https://github.com/crypto-browserify/md5.js