[Pkg-javascript-commits] [node-sha.js] 19/237: fix digest

Bastien Roucariès rouca at moszumanska.debian.org
Fri May 5 09:02:51 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 fdee30be69cc3fa58a25ae3e32d629251e0a005d
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date:   Fri Dec 27 08:19:37 2013 +0700

    fix digest
---
 hash.js | 44 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 9 deletions(-)

diff --git a/hash.js b/hash.js
index 2bfbc45..07ae790 100644
--- a/hash.js
+++ b/hash.js
@@ -1,11 +1,13 @@
 var u = require('./util')
-var hexpp = require('./hexpp')
+var hexpp = require('./hexpp').defaults({bigendian: true})
 
 module.exports = Hash
 
 //prototype class for hash functions
-function Hash (blockSize) {
+function Hash (blockSize, finalSize) {
   this._block = new Uint32Array(blockSize/4)
+  this._dv = new DataView(this._block.buffer)
+  this._finalSize = finalSize
   this._len = 0
   this._l = 0
 }
@@ -28,22 +30,46 @@ Hash.prototype.update = function (data, enc) {
   var f = 0
   while(s < l) {
     var t = Math.min(data.length, f + bl)
-    u.write(this._block.buffer, data, 'ascii', l%bl, f, t)
+    u.write(this._block.buffer, data, 'ascii', s%bl, f, t, true)
     s += (t - f)
-    if(!(s%bl))
+
+    if(!(s%bl)) {
       this._update(this._block.buffer)
+      u.zeroFill(this._block.buffer, 0)
+    }
+
   }
+  this._s = s
 
-  console.log('---WRITTEN---')
-  console.log(hexpp(this._block))
   return this
 
 }
 
 Hash.prototype.digest = function (enc) {
-  return u.toHex(this._final())
-  //reverse byte order, so that the individual bytes are in correct order.
-//  return u.toHex(this._hash.buffer)
+  //how much message is leftover
+  var bl = this._block.byteLength
+  var fl = this._finalSize
+  var len = this._len*8
+
+  var x = this._block.buffer
+  var X = this._dv
+
+  var bits = len % bl*8
+
+  //add end marker, so that appending 0's creats a different hash.
+  x[bits >> 4] = 0x80
+
+  console.log('--- final ---')
+  console.log(hexpp(x))
+
+  if(bits >= fl) {
+    this._update(this._block.buffer)
+    zeroFill(this._x, 0)
+  }
+
+  //TODO: handle case where the bit length is > Math.pow(2, 29)
+  X.setUint32(fl + 4, len, false) //big endian
+  return this._update(this._block.buffer)
 }
 
 Hash.prototype._update = function () {

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