[Pkg-javascript-commits] [node-hash-base] 06/13: Real example in README
Bastien Roucariès
rouca at moszumanska.debian.org
Sat Aug 12 22:00:44 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch upstream
in repository node-hash-base.
commit 71fc5bb4ac98a837bc68a34b58aa668e88031a33
Author: Kirill Fomichev <fanatid at ya.ru>
Date: Wed Nov 2 15:38:07 2016 +0300
Real example in README
---
README.md | 16 ++++++++++++----
index.js | 2 +-
2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
index 8d5b543..83ae2ed 100644
--- a/README.md
+++ b/README.md
@@ -11,21 +11,29 @@ Abstract base class to inherit from if you want to create streams implementing t
## Example
```js
-var HashBase = require('hash-base')
+const HashBase = require('hash-base')
+const inherits = require('inherits')
+// our hash function is XOR sum of all bytes
function MyHash () {
- HashBase.call(64) // in bytes
+ HashBase.call(this, 1) // in bytes
+
+ this._sum = 0x00
}
inherits(MyHash, HashBase)
MyHash.prototype._update = function () {
- // hashing one block with buffer this._block
+ for (let i = 0; i < this._block.length; ++i) this._sum ^= this._block[i]
}
MyHash.prototype._digest = function () {
- // create padding and produce result
+ return this._sum
}
+
+const data = Buffer.from([ 0x00, 0x42, 0x01 ])
+const hash = new MyHash().update(data).digest()
+console.log(hash) // => 67
```
You also can check [source code](index.js) or [crypto-browserify/md5.js][5]
diff --git a/index.js b/index.js
index de9e700..e455590 100644
--- a/index.js
+++ b/index.js
@@ -68,7 +68,7 @@ HashBase.prototype.update = function (data, encoding) {
return this
}
-HashBase.prototype._update = function (data) {
+HashBase.prototype._update = function () {
throw new Error('_update is not implemented')
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-hash-base.git
More information about the Pkg-javascript-commits
mailing list