[Pkg-javascript-commits] [node-sha.js] 171/237: improve sha* code structuring consistency

Bastien Roucariès rouca at moszumanska.debian.org
Fri May 5 09:03: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 d35623d4eddad7bc6f74c6c4f20e59cfc582215d
Author: Daniel Cousens <github at dcousens.com>
Date:   Mon Jan 12 16:56:00 2015 +1100

    improve sha* code structuring consistency
---
 hash.js   |   9 +----
 sha1.js   | 137 ++++++++++++++++++++++++--------------------------------------
 sha224.js |   9 +----
 sha256.js |  45 +++++++++------------
 sha384.js |   5 ---
 sha512.js |   5 ---
 6 files changed, 75 insertions(+), 135 deletions(-)

diff --git a/hash.js b/hash.js
index 49cb664..853ec06 100644
--- a/hash.js
+++ b/hash.js
@@ -1,5 +1,3 @@
-
-
 //prototype class for hash functions
 function Hash (blockSize, finalSize) {
   this._block = new Buffer(blockSize) //new Uint32Array(blockSize/4)
@@ -9,11 +7,6 @@ function Hash (blockSize, finalSize) {
   this._s = 0
 }
 
-Hash.prototype.init = function () {
-  this._s = 0
-  this._len = 0
-}
-
 Hash.prototype.update = function (data, enc) {
   if ("string" === typeof data) {
     enc = enc || "utf8"
@@ -21,7 +14,7 @@ Hash.prototype.update = function (data, enc) {
   }
 
   var l = this._len += data.length
-  var s = this._s = (this._s || 0)
+  var s = this._s || 0
   var f = 0
   var buffer = this._block
 
diff --git a/sha1.js b/sha1.js
index 4e947a2..032430c 100644
--- a/sha1.js
+++ b/sha1.js
@@ -8,29 +8,15 @@
  */
 
 var inherits = require('inherits')
-
 var Hash = require('./hash')
 
-var A = 0|0
-var B = 4|0
-var C = 8|0
-var D = 12|0
-var E = 16|0
-
-var W = new (typeof Int32Array === 'undefined' ? Array : Int32Array)(80)
-
-var POOL = []
+var W = new Array(80)
 
 function Sha1 () {
-  if(POOL.length)
-    return POOL.pop().init()
-
-  if(!(this instanceof Sha1)) return new Sha1()
+  this.init()
   this._w = W
-  Hash.call(this, 16*4, 14*4)
 
-  this._h = null
-  this.init()
+  Hash.call(this, 64, 56)
 }
 
 inherits(Sha1, Hash)
@@ -42,30 +28,53 @@ Sha1.prototype.init = function () {
   this._d = 0x10325476
   this._e = 0xc3d2e1f0
 
-  Hash.prototype.init.call(this)
   return this
 }
 
-Sha1.prototype._POOL = POOL
-Sha1.prototype._update = function (X) {
+/*
+ * Perform the appropriate triplet combination function for the current
+ * iteration
+ */
+function sha1_ft(t, b, c, d) {
+  if (t < 20) return (b & c) | ((~b) & d);
+  if (t < 40) return b ^ c ^ d;
+  if (t < 60) return (b & c) | (b & d) | (c & d);
+  return b ^ c ^ d;
+}
 
-  var a, b, c, d, e, _a, _b, _c, _d, _e
+/*
+ * Determine the appropriate additive constant for the current iteration
+ */
+function sha1_kt(t) {
+    return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :
+           (t < 60) ? -1894007588 : -899497514;
+}
+
+/*
+ * Bitwise rotate a 32-bit number to the left.
+ */
+function rol(num, cnt) {
+  return (num << cnt) | (num >>> (32 - cnt));
+}
 
-  a = _a = this._a
-  b = _b = this._b
-  c = _c = this._c
-  d = _d = this._d
-  e = _e = this._e
+Sha1.prototype._update = function (M) {
+  var W = this._w
+  var a, b, c, d, e
 
-  var w = this._w
+  a = this._a
+  b = this._b
+  c = this._c
+  d = this._d
+  e = this._e
 
-  for(var j = 0; j < 80; j++) {
-    var W = w[j] = j < 16 ? X.readInt32BE(j*4)
-      : rol(w[j - 3] ^ w[j -  8] ^ w[j - 14] ^ w[j - 16], 1)
+  for (var j = 0; j < 80; j++) {
+    var w = W[j] = j < 16
+      ? M.readInt32BE(j * 4)
+      : rol(W[j - 3] ^ W[j -  8] ^ W[j - 14] ^ W[j - 16], 1)
 
-    var t = add(
-      add(rol(a, 5), sha1_ft(j, b, c, d)),
-      add(add(e, W), sha1_kt(j))
+    var t = (
+      ((rol(a, 5) + sha1_ft(j, b, c, d)) | 0) +
+      ((((e + w) | 0) + sha1_kt(j)) | 0)
     )
 
     e = d
@@ -75,63 +84,23 @@ Sha1.prototype._update = function (X) {
     a = t
   }
 
-  this._a = add(a, _a)
-  this._b = add(b, _b)
-  this._c = add(c, _c)
-  this._d = add(d, _d)
-  this._e = add(e, _e)
+  this._a = (a + this._a) | 0
+  this._b = (b + this._b) | 0
+  this._c = (c + this._c) | 0
+  this._d = (d + this._d) | 0
+  this._e = (e + this._e) | 0
 }
 
 Sha1.prototype._hash = function () {
-  if(POOL.length < 100) POOL.push(this)
   var H = new Buffer(20)
-  //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
-}
 
-/*
- * Perform the appropriate triplet combination function for the current
- * iteration
- */
-function sha1_ft(t, b, c, d) {
-  if(t < 20) return (b & c) | ((~b) & d);
-  if(t < 40) return b ^ c ^ d;
-  if(t < 60) return (b & c) | (b & d) | (c & d);
-  return b ^ c ^ d;
-}
+  H.writeInt32BE(this._a|0, 0)
+  H.writeInt32BE(this._b|0, 4)
+  H.writeInt32BE(this._c|0, 8)
+  H.writeInt32BE(this._d|0, 12)
+  H.writeInt32BE(this._e|0, 16)
 
-/*
- * Determine the appropriate additive constant for the current iteration
- */
-function sha1_kt(t) {
-  return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :
-         (t < 60) ? -1894007588 : -899497514;
-}
-
-/*
- * Add integers, wrapping at 2^32. This uses 16-bit operations internally
- * to work around bugs in some JS interpreters.
- * //dominictarr: this is 10 years old, so maybe this can be dropped?)
- *
- */
-function add(x, y) {
-  return (x + y ) | 0
-//lets see how this goes on testling.
-//  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
-//  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
-//  return (msw << 16) | (lsw & 0xFFFF);
-}
-
-/*
- * Bitwise rotate a 32-bit number to the left.
- */
-function rol(num, cnt) {
-  return (num << cnt) | (num >>> (32 - cnt));
+  return H
 }
 
 module.exports = Sha1
diff --git a/sha224.js b/sha224.js
index 852087c..b8b4426 100644
--- a/sha224.js
+++ b/sha224.js
@@ -1,4 +1,3 @@
-
 /**
  * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
  * in FIPS 180-2
@@ -16,15 +15,14 @@ var W = new Array(64)
 function Sha224() {
   this.init()
 
-  this._w = W //new Array(64)
+  this._w = W // new Array(64)
 
-  Hash.call(this, 16*4, 14*4)
+  Hash.call(this, 64, 56)
 }
 
 inherits(Sha224, SHA256)
 
 Sha224.prototype.init = function () {
-
   this._a = 0xc1059ed8|0
   this._b = 0x367cd507|0
   this._c = 0x3070dd17|0
@@ -34,12 +32,9 @@ Sha224.prototype.init = function () {
   this._g = 0x64f98fa7|0
   this._h = 0xbefa4fa4|0
 
-  this._len = this._s = 0
-
   return this
 }
 
-
 Sha224.prototype._hash = function () {
   var H = new Buffer(28)
 
diff --git a/sha256.js b/sha256.js
index 0b7e800..0e1efc6 100644
--- a/sha256.js
+++ b/sha256.js
@@ -1,4 +1,3 @@
-
 /**
  * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
  * in FIPS 180-2
@@ -8,42 +7,40 @@
  */
 
 var inherits = require('inherits')
-
 var Hash = require('./hash')
 
 var K = [
-    0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
-    0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
-    0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
-    0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
-    0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
-    0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
-    0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
-    0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
-    0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
-    0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
-    0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
-    0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
-    0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
-    0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
-    0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
-    0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
-  ]
+  0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
+  0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
+  0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
+  0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
+  0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
+  0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
+  0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
+  0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
+  0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
+  0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
+  0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
+  0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
+  0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
+  0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
+  0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
+  0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
+]
 
 var W = new Array(64)
 
 function Sha256() {
   this.init()
 
-  this._w = W //new Array(64)
+  this._w = W // new Array(64)
 
-  Hash.call(this, 16*4, 14*4)
+  Hash.call(this, 64, 56)
 }
 
 inherits(Sha256, Hash)
 
 Sha256.prototype.init = function () {
-
   this._a = 0x6a09e667|0
   this._b = 0xbb67ae85|0
   this._c = 0x3c6ef372|0
@@ -53,8 +50,6 @@ Sha256.prototype.init = function () {
   this._g = 0x1f83d9ab|0
   this._h = 0x5be0cd19|0
 
-  this._len = this._s = 0
-
   return this
 }
 
@@ -91,7 +86,6 @@ function Gamma1256 (x) {
 }
 
 Sha256.prototype._update = function(M) {
-
   var W = this._w
   var a, b, c, d, e, f, g, h
   var T1, T2
@@ -124,7 +118,6 @@ Sha256.prototype._update = function(M) {
   this._f = (f + this._f) | 0
   this._g = (g + this._g) | 0
   this._h = (h + this._h) | 0
-
 };
 
 Sha256.prototype._hash = function () {
diff --git a/sha384.js b/sha384.js
index 0b07c99..5966e5d 100644
--- a/sha384.js
+++ b/sha384.js
@@ -14,7 +14,6 @@ function Sha384() {
 inherits(Sha384, SHA512)
 
 Sha384.prototype.init = function () {
-
   this._a = 0xcbbb9d5d|0
   this._b = 0x629a292a|0
   this._c = 0x9159015a|0
@@ -33,13 +32,9 @@ Sha384.prototype.init = function () {
   this._gl = 0x64f98fa7|0
   this._hl = 0xbefa4fa4|0
 
-  this._len = this._s = 0
-
   return this
 }
 
-
-
 Sha384.prototype._hash = function () {
   var H = new Buffer(48)
 
diff --git a/sha512.js b/sha512.js
index fa55df0..24a68d4 100644
--- a/sha512.js
+++ b/sha512.js
@@ -1,5 +1,4 @@
 var inherits = require('inherits')
-
 var Hash = require('./hash')
 
 var K = [
@@ -57,7 +56,6 @@ function Sha512() {
 inherits(Sha512, Hash)
 
 Sha512.prototype.init = function () {
-
   this._a = 0x6a09e667|0
   this._b = 0xbb67ae85|0
   this._c = 0x3c6ef372|0
@@ -76,8 +74,6 @@ Sha512.prototype.init = function () {
   this._gl = 0xfb41bd6b|0
   this._hl = 0x137e2179|0
 
-  this._len = this._s = 0
-
   return this
 }
 
@@ -94,7 +90,6 @@ function Maj (x, y, z) {
 }
 
 Sha512.prototype._update = function(M) {
-
   var W = this._w
   var a, b, c, d, e, f, g, h
   var al, bl, cl, dl, el, fl, gl, hl

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