[Pkg-javascript-commits] [node-create-hash] 10/40: avoid using stream when using update or digest methods
Bastien Roucariès
rouca at moszumanska.debian.org
Sat May 27 14:10:34 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-create-hash.
commit 3868b8c45cb6ba96a1d41e42d6cfe53f5644eefd
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date: Mon Jan 26 18:24:45 2015 -0500
avoid using stream when using update or digest methods
---
browser.js | 49 +++++++++++++++++++++++--------------------------
1 file changed, 23 insertions(+), 26 deletions(-)
diff --git a/browser.js b/browser.js
index 62fc416..3240de8 100644
--- a/browser.js
+++ b/browser.js
@@ -1,15 +1,15 @@
'use strict';
-var createHash = require('sha.js')
+var sha = require('sha.js')
var md5 = require('./md5')
var rmd160 = require('ripemd160')
var Transform = require('stream').Transform;
var inherits = require('inherits')
-module.exports = function (alg) {
+module.exports = function createHash (alg) {
if('md5' === alg) return new HashNoConstructor(md5)
if('rmd160' === alg) return new HashNoConstructor(rmd160)
- return new Hash(createHash(alg))
+ return new Hash(sha(alg))
}
inherits(HashNoConstructor, Transform)
@@ -24,38 +24,37 @@ HashNoConstructor.prototype._transform = function (data, _, done) {
done()
}
HashNoConstructor.prototype._flush = function (done) {
- var buf = Buffer.concat(this.buffers)
- var r = this._hash(buf)
- this.buffers = null
- this.push(r)
+ this.push(this.digest())
done()
}
HashNoConstructor.prototype.update = function (data, enc) {
- this.write(data, enc)
+ if (typeof data === 'string') {
+ data = new Buffer(data, enc)
+ }
+ this.buffers.push(data)
return this
}
HashNoConstructor.prototype.digest = function (enc) {
- this.end()
- var outData = new Buffer('')
- var chunk
- while ((chunk = this.read())) {
- outData = Buffer.concat([outData, chunk])
- }
+ var buf = Buffer.concat(this.buffers)
+ var r = this._hash(buf)
+ this.buffers = null
if (enc) {
- outData = outData.toString(enc)
+ r = r.toString(enc)
}
- return outData
+ return r
}
inherits(Hash, Transform)
function Hash(hash) {
- Transform.call(this);
+ Transform.call(this)
this._hash = hash
}
-
-Hash.prototype._transform = function (data, _, done) {
+Hash.prototype._transform = function (data, enc, done) {
+ if (enc) {
+ data = new Buffer(data, enc)
+ }
this._hash.update(data)
done()
}
@@ -65,17 +64,15 @@ Hash.prototype._flush = function (done) {
done()
}
Hash.prototype.update = function (data, enc) {
- this.write(data, enc)
+ if (typeof data === 'string') {
+ data = new Buffer(data, enc)
+ }
+ this._hash.update(data)
return this
}
Hash.prototype.digest = function (enc) {
- this.end()
- var outData = new Buffer('')
- var chunk
- while ((chunk = this.read())) {
- outData = Buffer.concat([outData, chunk])
- }
+ var outData = this._hash.digest()
if (enc) {
outData = outData.toString(enc)
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-create-hash.git
More information about the Pkg-javascript-commits
mailing list