[Pkg-javascript-commits] [node-sha.js] 99/237: inject Buffer dep into hash
Bastien Roucariès
rouca at moszumanska.debian.org
Fri May 5 09:03:39 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-sha.js.
commit 21df55938c274a75d5d04dfd7a4c9abe41d0ce7c
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date: Wed Jan 15 13:11:49 2014 +0700
inject Buffer dep into hash
---
hash.js | 158 ++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 78 insertions(+), 80 deletions(-)
diff --git a/hash.js b/hash.js
index c14f704..377cfb6 100644
--- a/hash.js
+++ b/hash.js
@@ -1,98 +1,96 @@
var u = require('./util')
var write = u.write
var fill = u.zeroFill
-var Buffer = require('./fakebuffer')
-var toBuffer = require('bops/typedarray/from')
-
-module.exports = Hash
-
-//prototype class for hash functions
-function Hash (blockSize, finalSize) {
- this._block = new Buffer(blockSize) //new Uint32Array(blockSize/4)
-// this._dv = new DataView(this._block.buffer)
- this._finalSize = finalSize
- this._blockSize = blockSize
- this._len = 0
- this._l = 0
-}
-function lengthOf(data, enc) {
- if(enc == null) return data.byteLength || data.length
- if(enc == 'ascii' || enc == 'binary') return data.length
- if(enc == 'hex') return data.length/2
- if(enc == 'base64') return data.length/3
-}
+module.exports = function (Buffer) {
-Hash.prototype.update = function (data, enc) {
- //for encoding/decoding utf8, see here:
- //https://github.com/chrisdickinson/bops/blob/master/typedarray/from.js#L36-L57
- //https://github.com/chrisdickinson/to-utf8
- var bl = this._blockSize
- //for now, assume ascii.
-
- //I'd rather do this with a streaming encoder, like the opposite of
- //http://nodejs.org/api/string_decoder.html
- var length
- if(!enc && 'string' === typeof data)
- enc = 'utf8'
-
- if(enc) {
- if(enc === 'utf-8')
- enc = 'utf8'
-
- if(enc === 'base64' || enc === 'utf8')
- data = toBuffer(data, enc), enc = null
- length = lengthOf(data, enc)
- } else
- length = data.byteLength || data.length
-
- var l = this._len += length
- var s = this._s = (this._s || 0)
- var f = 0
- var buffer = this._block.buffer || this._block
- while(s < l) {
- var t = Math.min(length, f + bl)
- write(buffer, data, enc, s%bl, f, t)
- var ch = (t - f);
- s += ch; f += ch
-
- if(!(s%bl))
- this._update(buffer)
+ //prototype class for hash functions
+ function Hash (blockSize, finalSize) {
+ this._block = new Buffer(blockSize) //new Uint32Array(blockSize/4)
+ this._finalSize = finalSize
+ this._blockSize = blockSize
+ this._len = 0
+ this._l = 0
}
- this._s = s
- return this
+ function lengthOf(data, enc) {
+ if(enc == null) return data.byteLength || data.length
+ if(enc == 'ascii' || enc == 'binary') return data.length
+ if(enc == 'hex') return data.length/2
+ if(enc == 'base64') return data.length/3
+ }
-}
+ Hash.prototype.update = function (data, enc) {
+ var bl = this._blockSize
+ //for now, assume ascii.
+
+ //I'd rather do this with a streaming encoder, like the opposite of
+ //http://nodejs.org/api/string_decoder.html
+ var length
+ if(!enc && 'string' === typeof data)
+ enc = 'utf8'
-Hash.prototype.digest = function (enc) {
- //how much message is leftover
- var bl = this._blockSize
- var fl = this._finalSize
- var len = this._len*8
+ if(enc) {
+ if(enc === 'utf-8')
+ enc = 'utf8'
- var x = this._block
-// var X = this._dv
+ if(enc === 'base64' || enc === 'utf8')
+ data = new Buffer(data, enc), enc = null
- var bits = len % (bl*8)
+ length = lengthOf(data, enc)
+ } else
+ length = data.byteLength || data.length
- //add end marker, so that appending 0's creats a different hash.
- x[this._len % bl] = 0x80
- fill(this._block, this._len % bl + 1)
+ var l = this._len += length
+ var s = this._s = (this._s || 0)
+ var f = 0
+ var buffer = this._block
+ while(s < l) {
+ var t = Math.min(length, f + bl)
+ write(buffer, data, enc, s%bl, f, t)
+ var ch = (t - f);
+ s += ch; f += ch
+
+ if(!(s%bl))
+ this._update(buffer)
+ }
+ this._s = s
+
+ return this
- if(bits >= fl*8) {
- this._update(this._block)
- u.zeroFill(this._block, 0)
}
- //TODO: handle case where the bit length is > Math.pow(2, 29)
- x.writeInt32BE(len, fl + 4) //big endian
+ Hash.prototype.digest = function (enc) {
+ //how much message is leftover
+ var bl = this._blockSize
+ var fl = this._finalSize
+ var len = this._len*8
- var hash = this._update(this._block) || this._hash()
- return u.toString(hash, enc)
-}
+ var x = this._block
-Hash.prototype._update = function () {
- throw new Error('_update must be implemented by subclass')
-}
+ var bits = len % (bl*8)
+ //add end marker, so that appending 0's creats a different hash.
+ x[this._len % bl] = 0x80
+ fill(this._block, this._len % bl + 1)
+
+ if(bits >= fl*8) {
+ this._update(this._block)
+ u.zeroFill(this._block, 0)
+ }
+
+ //TODO: handle case where the bit length is > Math.pow(2, 29)
+ x.writeInt32BE(len, fl + 4) //big endian
+
+ var hash = this._update(this._block) || this._hash()
+ if(enc == null) return hash
+ return u.toString(hash, enc)
+ }
+
+ Hash.prototype._update = function () {
+ throw new Error('_update must be implemented by subclass')
+ }
+
+ return Hash
+
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-sha.js.git
More information about the Pkg-javascript-commits
mailing list