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