[Pkg-javascript-commits] [node-hash.js] 14/29: lib: less allocations
Bastien Roucariès
rouca at moszumanska.debian.org
Thu Apr 20 19:30: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-hash.js.
commit ef2e6bc4e513c47302eebb7a10eaae3d35b7bd97
Author: Fedor Indutny <fedor at indutny.com>
Date: Sun Aug 31 14:36:38 2014 +0400
lib: less allocations
---
lib/hash/common.js | 13 ++++++++-----
lib/hash/hmac.js | 15 +++++++++------
lib/hash/ripemd.js | 6 +++---
lib/hash/sha.js | 11 ++++++-----
4 files changed, 26 insertions(+), 19 deletions(-)
diff --git a/lib/hash/common.js b/lib/hash/common.js
index 1e0506a..292d4b8 100644
--- a/lib/hash/common.js
+++ b/lib/hash/common.js
@@ -9,6 +9,9 @@ function BlockHash() {
this.outSize = this.constructor.outSize;
this.hmacStrength = this.constructor.hmacStrength;
this.endian = 'big';
+
+ this._delta8 = this.blockSize / 8;
+ this._delta32 = this.blockSize / 32;
}
exports.BlockHash = BlockHash;
@@ -22,18 +25,18 @@ BlockHash.prototype.update = function update(msg, enc) {
this.pendingTotal += msg.length;
// Enough data, try updating
- if (this.pending.length >= this.blockSize / 8) {
+ if (this.pending.length >= this._delta8) {
msg = this.pending;
// Process pending data in blocks
- var r = msg.length % (this.blockSize / 8);
+ var r = msg.length % this._delta8;
this.pending = msg.slice(msg.length - r, msg.length);
if (this.pending.length === 0)
this.pending = null;
msg = utils.join32(msg.slice(0, msg.length - r), this.endian);
- for (var i = 0; i < msg.length; i += this.blockSize / 32)
- this._update(msg.slice(i, i + this.blockSize / 32));
+ for (var i = 0; i < msg.length; i += this._delta32)
+ this._update(msg, i, i + this._delta32);
}
return this;
@@ -48,7 +51,7 @@ BlockHash.prototype.digest = function digest(enc) {
BlockHash.prototype._pad = function pad() {
var len = this.pendingTotal;
- var bytes = this.blockSize / 8;
+ var bytes = this._delta8;
var k = bytes - ((len + 8) % bytes);
var res = new Array(k + 8);
res[0] = 0x80;
diff --git a/lib/hash/hmac.js b/lib/hash/hmac.js
index d48f3eb..9c09793 100644
--- a/lib/hash/hmac.js
+++ b/lib/hash/hmac.js
@@ -24,15 +24,18 @@ Hmac.prototype._init = function init(key) {
for (var i = key.length; i < this.blockSize; i++)
key.push(0);
- var okey = key.slice();
- for (var i = 0; i < key.length; i++) {
+ for (var i = 0; i < key.length; i++)
key[i] ^= 0x36;
- okey[i] ^= 0x5c;
- }
+ var 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: new this.Hash().update(key),
- outer: new this.Hash().update(okey)
+ inner: inner,
+ outer: outer
};
};
diff --git a/lib/hash/ripemd.js b/lib/hash/ripemd.js
index 7f569ed..54aed01 100644
--- a/lib/hash/ripemd.js
+++ b/lib/hash/ripemd.js
@@ -23,7 +23,7 @@ RIPEMD160.blockSize = 512;
RIPEMD160.outSize = 160;
RIPEMD160.hmacStrength = 192;
-RIPEMD160.prototype._update = function update(msg) {
+RIPEMD160.prototype._update = function update(msg, start) {
var A = this.h[0];
var B = this.h[1];
var C = this.h[2];
@@ -37,7 +37,7 @@ RIPEMD160.prototype._update = function update(msg) {
for (var j = 0; j < 80; j++) {
var T = sum32(
rotl32(
- sum32_4(A, f(j, B, C, D), msg[r[j]], K(j)),
+ sum32_4(A, f(j, B, C, D), msg[r[j] + start], K(j)),
s[j]),
E);
A = E;
@@ -47,7 +47,7 @@ RIPEMD160.prototype._update = function update(msg) {
B = T;
T = sum32(
rotl32(
- sum32_4(Ah, f(79 - j, Bh, Ch, Dh), msg[rh[j]], Kh(j)),
+ sum32_4(Ah, f(79 - j, Bh, Ch, Dh), msg[rh[j] + start], Kh(j)),
sh[j]),
Eh);
Ah = Eh;
diff --git a/lib/hash/sha.js b/lib/hash/sha.js
index e2553ea..bfa7d38 100644
--- a/lib/hash/sha.js
+++ b/lib/hash/sha.js
@@ -49,10 +49,10 @@ SHA256.blockSize = 512;
SHA256.outSize = 256;
SHA256.hmacStrength = 192;
-SHA256.prototype._update = function _update(msg) {
+SHA256.prototype._update = function _update(msg, start) {
var W = new Array(64);
for (var i = 0; i < 16; i++)
- W[i] = msg[i];
+ W[i] = msg[start + i];
for (; i < W.length; i++)
W[i] = sum32_4(g1_256(W[i - 2]), W[i - 7], g0_256(W[i - 15]), W[i - 16]);
@@ -126,6 +126,7 @@ function SHA1() {
BlockHash.call(this);
this.h = [ 0x67452301, 0xefcdab89, 0x98badcfe,
0x10325476, 0xc3d2e1f0 ];
+ this.W = Array(80);
}
utils.inherits(SHA1, BlockHash);
@@ -135,11 +136,11 @@ SHA1.blockSize = 512;
SHA1.outSize = 160;
SHA1.hmacStrength = 80;
-SHA1.prototype._update = function _update(msg) {
- var W = Array(80);
+SHA1.prototype._update = function _update(msg, start) {
+ var W = this.W;
for (var i = 0; i < 16; i++)
- W[i] = msg[i];
+ W[i] = msg[start + i];
for(; i < W.length; i++)
W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
--
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