[Pkg-javascript-commits] [node-hash.js] 15/29: lib: further improvements

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:30: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-hash.js.

commit 2285c441845ab79325ae9b0461ef73dcd01eba54
Author: Fedor Indutny <fedor at indutny.com>
Date:   Sun Aug 31 14:41:05 2014 +0400

    lib: further improvements
---
 lib/hash/common.js |  2 +-
 lib/hash/hmac.js   | 18 ++++++++----------
 lib/hash/sha.js    |  6 ++++--
 lib/hash/utils.js  |  9 +++++----
 4 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/lib/hash/common.js b/lib/hash/common.js
index 292d4b8..c175d40 100644
--- a/lib/hash/common.js
+++ b/lib/hash/common.js
@@ -34,7 +34,7 @@ BlockHash.prototype.update = function update(msg, enc) {
     if (this.pending.length === 0)
       this.pending = null;
 
-    msg = utils.join32(msg.slice(0, msg.length - r), this.endian);
+    msg = utils.join32(msg, 0, msg.length - r, this.endian);
     for (var i = 0; i < msg.length; i += this._delta32)
       this._update(msg, i, i + this._delta32);
   }
diff --git a/lib/hash/hmac.js b/lib/hash/hmac.js
index 9c09793..3a3da97 100644
--- a/lib/hash/hmac.js
+++ b/lib/hash/hmac.js
@@ -10,6 +10,9 @@ function Hmac(hash, key, enc) {
   this.Hash = hash;
   this.blockSize = hash.blockSize / 8;
   this.outSize = hash.outSize / 8;
+  this.inner = null;
+  this.outer = null;
+
   this._init(utils.toArray(key, enc));
 }
 module.exports = Hmac;
@@ -26,25 +29,20 @@ Hmac.prototype._init = function init(key) {
 
   for (var i = 0; i < key.length; i++)
     key[i] ^= 0x36;
-  var inner = new this.Hash().update(key);
+  this.inner = new this.Hash().update(key);
 
   // 0x36 ^ 0x5c = 0x6a
   for (var i = 0; i < key.length; i++)
     key[i] ^= 0x6a;
-  var outer = new this.Hash().update(key);
-
-  this.hash = {
-    inner: inner,
-    outer: outer
-  };
+  this.outer = new this.Hash().update(key);
 };
 
 Hmac.prototype.update = function update(msg, enc) {
-  this.hash.inner.update(msg, enc);
+  this.inner.update(msg, enc);
   return this;
 };
 
 Hmac.prototype.digest = function digest(enc) {
-  this.hash.outer.update(this.hash.inner.digest());
-  return this.hash.outer.digest(enc);
+  this.outer.update(this.inner.digest());
+  return this.outer.digest(enc);
 };
diff --git a/lib/hash/sha.js b/lib/hash/sha.js
index bfa7d38..b03e299 100644
--- a/lib/hash/sha.js
+++ b/lib/hash/sha.js
@@ -41,6 +41,7 @@ function SHA256() {
   this.h = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
              0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ];
   this.k = sha256_K;
+  this.W = new Array(64);
 }
 utils.inherits(SHA256, BlockHash);
 exports.sha256 = SHA256;
@@ -50,7 +51,8 @@ SHA256.outSize = 256;
 SHA256.hmacStrength = 192;
 
 SHA256.prototype._update = function _update(msg, start) {
-  var W = new Array(64);
+  var W = this.W;
+
   for (var i = 0; i < 16; i++)
     W[i] = msg[start + i];
   for (; i < W.length; i++)
@@ -126,7 +128,7 @@ function SHA1() {
   BlockHash.call(this);
   this.h = [ 0x67452301, 0xefcdab89, 0x98badcfe,
              0x10325476, 0xc3d2e1f0 ];
-  this.W = Array(80);
+  this.W = new Array(80);
 }
 
 utils.inherits(SHA1, BlockHash);
diff --git a/lib/hash/utils.js b/lib/hash/utils.js
index d00a0e8..b077716 100644
--- a/lib/hash/utils.js
+++ b/lib/hash/utils.js
@@ -87,10 +87,11 @@ function zero8(word) {
 }
 utils.zero8 = zero8;
 
-function join32(msg, endian) {
-  assert(msg.length % 4 === 0);
-  var res = new Array(msg.length / 4);
-  for (var i = 0, k = 0; i < res.length; i++, k += 4) {
+function join32(msg, start, end, endian) {
+  var len = end - start;
+  assert(len % 4 === 0);
+  var res = new Array(len / 4);
+  for (var i = 0, k = start; i < res.length; i++, k += 4) {
     var w;
     if (endian === 'big')
       w = (msg[k] << 24) | (msg[k + 1] << 16) | (msg[k + 2] << 8) | msg[k + 3];

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-hash.js.git



More information about the Pkg-javascript-commits mailing list