[Pkg-javascript-commits] [node-sha.js] 88/237: experiment using node buffers

Bastien Roucariès rouca at moszumanska.debian.org
Fri May 5 09:03:38 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 27f676750b9e6e042d2d2ea11ef2c4196f3b417c
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date:   Sat Jan 11 22:52:51 2014 +0700

    experiment using node buffers
---
 hash.js | 20 ++++++++++----------
 sha1.js | 38 ++++++++++++++++++++++----------------
 util.js |  4 +++-
 3 files changed, 35 insertions(+), 27 deletions(-)

diff --git a/hash.js b/hash.js
index c860065..e42e81b 100644
--- a/hash.js
+++ b/hash.js
@@ -7,8 +7,8 @@ module.exports = Hash
 
 //prototype class for hash functions
 function Hash (blockSize, finalSize) {
-  this._block = new Uint32Array(blockSize/4)
-  this._dv = new DataView(this._block.buffer)
+  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
@@ -48,7 +48,7 @@ Hash.prototype.update = function (data, enc) {
   var l = this._len += length
   var s = this._s = (this._s || 0)
   var f = 0
-  var buffer = this._block.buffer
+  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)
@@ -70,8 +70,8 @@ Hash.prototype.digest = function (enc) {
   var fl = this._finalSize
   var len = this._len*8
 
-  var x = this._block.buffer
-  var X = this._dv
+  var x = this._block
+//  var X = this._dv
 
   var bits = len % (bl*8)
 
@@ -79,18 +79,18 @@ Hash.prototype.digest = function (enc) {
   //console.log('--- final ---', bits, fl, this._len % bl, fl + 4, fl*8, bits >= fl*8)
   //console.log(hexpp(x))
   x[this._len % bl] = 0x80
-  fill(this._block.buffer, this._len % bl + 1)
+  fill(this._block, this._len % bl + 1)
   
   if(bits >= fl*8) {
-    this._update(this._block.buffer)
+    this._update(this._block)
     u.zeroFill(this._block, 0)
   }
 
   //TODO: handle case where the bit length is > Math.pow(2, 29)
-  X.setUint32(fl + 4, len, false) //big endian
+  x.writeInt32BE(len, fl + 4) //big endian
 
-  var hash = this._update(this._block.buffer) || this._hash()
-  return u.toString(new Uint8Array(hash.buffer || hash), enc)
+  var hash = this._update(this._block) || this._hash()
+  return u.toString(hash, enc)
 }
 
 Hash.prototype._update = function () {
diff --git a/sha1.js b/sha1.js
index 984f741..d003e73 100644
--- a/sha1.js
+++ b/sha1.js
@@ -11,13 +11,15 @@ module.exports = Sha1
 var inherits = require('util').inherits
 var Hash = require('./hash')
 
+var hexpp = require('./hexpp')
+
 inherits(Sha1, Hash)
 
-var A = 0
-var B = 4
-var C = 8
-var D = 12
-var E = 16
+var A = 0|0
+var B = 4|0
+var C = 8|0
+var D = 12|0
+var E = 16|0
 
 var BE = false
 var LE = true
@@ -28,9 +30,9 @@ function Sha1 () {
   this._w = new Uint32Array(80)
   Hash.call(this, 16*4, 14*4)
   
-  this._h = new Uint8Array(20)
-  var H = this._dvH = new DataView(this._h.buffer)
-  this._h32 = new Uint32Array(this._h.buffer)
+  this._h = new Buffer(20) //new Uint8Array(20)
+  var H = this._dvH = this._h // = new DataView(this._h.buffer)
+//  this._h32 = new Uint32Array(this._h.buffer)
 
   this._a = 0x67452301
   this._b = 0xefcdab89
@@ -47,8 +49,10 @@ function Sha1 () {
 
 Sha1.prototype._update = function (array) {
 
-  var X = this._dv
-  var H = this._dvH
+  var X = this._block
+//  var H = this._dvH
+
+//  console.log(hexpp(X))
 
   var h = this._h
   var a, b, c, d, e, _a, _b, _c, _d, _e
@@ -64,7 +68,7 @@ Sha1.prototype._update = function (array) {
   for(var j = 0; j < 80; j++) {
     var W = w[j]
       = j < 16
-      ? X.getUint32(j*4, BE)
+      ? X.readInt32BE(j*4)
       : rol(w[j - 3] ^ w[j -  8] ^ w[j - 14] ^ w[j - 16], 1)
 
     var t =
@@ -89,11 +93,13 @@ Sha1.prototype._update = function (array) {
 
 Sha1.prototype._hash = function () {
   var H = this._dvH //new DataView(new ArrayBuffer(20))
-  H.setUint32(A, this._a, BE)
-  H.setUint32(B, this._b, BE)
-  H.setUint32(C, this._c, BE)
-  H.setUint32(D, this._d, BE)
-  H.setUint32(E, this._e, BE)
+
+  //console.log(this._a|0, this._b|0, this._c|0, this._d|0, this._e|0)
+  H.writeInt32BE(this._a|0, A)
+  H.writeInt32BE(this._b|0, B)
+  H.writeInt32BE(this._c|0, C)
+  H.writeInt32BE(this._d|0, D)
+  H.writeInt32BE(this._e|0, E)
   return H
 }
 
diff --git a/util.js b/util.js
index be088ba..1162704 100644
--- a/util.js
+++ b/util.js
@@ -67,11 +67,13 @@ function toBinary (buf) {
 
 //always fill to the end!
 function zeroFill(buf, from) {
-  for(var i = from; i < buf.byteLength; i++)
+  for(var i = from; i < buf.length; i++)
     buf[i] = 0
 }
 
 function toString(buf, enc) {
+  if(Buffer.isBuffer(buf))
+    return buf.toString(enc)
   if(null == enc) return buf
   if('hex' == enc)
     return toHex(buf)

-- 
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