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