[Pkg-javascript-commits] [node-sha.js] 12/31: hash: remove repeated remainder calculation

Bastien Roucariès rouca at moszumanska.debian.org
Thu Nov 30 11:23:26 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 158bc835fbffbbd80f93c97ba0db8e7da7db9c0e
Author: Daniel Cousens <github at dcousens.com>
Date:   Fri May 19 19:55:08 2017 +1000

    hash: remove repeated remainder calculation
---
 hash.js | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/hash.js b/hash.js
index 0841769..7c07d07 100644
--- a/hash.js
+++ b/hash.js
@@ -41,27 +41,32 @@ Hash.prototype.update = function (data, enc) {
 }
 
 Hash.prototype.digest = function (enc) {
-  // Suppose the length of the message M, in bits, is l
-  var l = this._len * 8
+  var rem = this._len % this._blockSize
 
-  // Append the bit 1 to the end of the message
-  this._block[this._len % this._blockSize] = 0x80
+  this._block[rem] = 0x80
 
-  // and then k zero bits, where k is the smallest non-negative solution to the equation (l + 1 + k) === finalSize mod blockSize
-  this._block.fill(0, this._len % this._blockSize + 1)
+  // zero (rem + 1) trailing bits, where (rem + 1) is the smallest
+  // non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
+  this._block.fill(0, rem + 1)
 
-  if (l % (this._blockSize * 8) >= this._finalSize * 8) {
+  if (rem >= this._finalSize) {
     this._update(this._block)
     this._block.fill(0)
   }
 
-  // to this append the block which is equal to the number l written in binary
-  if (l <= 0xffffffff) {
-    this._block.writeUInt32BE(l, this._blockSize - 4)
+  var bits = this._len * 8
+
+  // uint32
+  if (bits <= 0xffffffff) {
+    this._block.writeUInt32BE(bits, this._blockSize - 4)
+
+  // uint64
   } else {
-    var ll = l & 0xffffffff
-    this._block.writeUInt32BE((l - ll) / 0x100000000, this._blockSize - 8)
-    this._block.writeUInt32BE(ll, this._blockSize - 4)
+    var lowBits = bits & 0xffffffff
+    var highBits = (bits - lowBits) / 0x100000000
+
+    this._block.writeUInt32BE(highBits, this._blockSize - 8)
+    this._block.writeUInt32BE(lowBits, this._blockSize - 4)
   }
 
   this._update(this._block)

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