13c5493bc32a5a714fb1bcf9a71e97c4a9241042b122e4fff4e898427145ca14905c8ea7021a84c4c154cfffcca31a984f2957a7c0eddaab04dcd67d7468cb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. 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]).
  6. ## Example
  7. ```js
  8. const HashBase = require('hash-base');
  9. const inherits = require('inherits');
  10. // our hash function is XOR sum of all bytes
  11. function MyHash () {
  12. HashBase.call(this, 1); // in bytes
  13. this._sum = 0x00;
  14. };
  15. inherits(MyHash, HashBase)
  16. MyHash.prototype._update = function () {
  17. for (let i = 0; i < this._block.length; ++i) {
  18. this._sum ^= this._block[i];
  19. }
  20. };
  21. MyHash.prototype._digest = function () {
  22. return this._sum;
  23. };
  24. const data = Buffer.from([0x00, 0x42, 0x01]);
  25. const hash = new MyHash().update(data).digest();
  26. console.log(hash); // => 67
  27. ```
  28. You also can check [source code](index.js) or [crypto-browserify/md5.js][5]
  29. ## LICENSE
  30. MIT
  31. [1]: https://nodejs.org/api/crypto.html#crypto_class_hash
  32. [2]: https://nodejs.org/api/crypto.html#crypto_class_cipher
  33. [3]: https://nodejs.org/api/crypto.html#crypto_class_decipher
  34. [4]: https://github.com/crypto-browserify/cipher-base
  35. [5]: https://github.com/crypto-browserify/md5.js