[Pkg-javascript-commits] [node-hash.js] 17/29: sha: sha512/sha384 support

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 bedc91460ad55a7c6542b2050e6fd9be6d9f7b36
Author: Fedor Indutny <fedor at indutny.com>
Date:   Mon Jan 5 23:47:38 2015 +0300

    sha: sha512/sha384 support
    
    Fix #3
---
 lib/hash.js        |   2 +
 lib/hash/common.js |  13 +++-
 lib/hash/ripemd.js |   1 +
 lib/hash/sha.js    | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 lib/hash/utils.js  | 147 +++++++++++++++++++++++++++++++++++++++---
 test/hash-test.js  | 123 ++++++++++++++++-------------------
 6 files changed, 392 insertions(+), 80 deletions(-)

diff --git a/lib/hash.js b/lib/hash.js
index 56c611b..f59b673 100644
--- a/lib/hash.js
+++ b/lib/hash.js
@@ -10,4 +10,6 @@ hash.hmac = require('./hash/hmac');
 hash.sha1 = hash.sha.sha1;
 hash.sha256 = hash.sha.sha256;
 hash.sha224 = hash.sha.sha224;
+hash.sha384 = hash.sha.sha384;
+hash.sha512 = hash.sha.sha512;
 hash.ripemd160 = hash.ripemd.ripemd160;
diff --git a/lib/hash/common.js b/lib/hash/common.js
index c175d40..a052c55 100644
--- a/lib/hash/common.js
+++ b/lib/hash/common.js
@@ -8,6 +8,7 @@ function BlockHash() {
   this.blockSize = this.constructor.blockSize;
   this.outSize = this.constructor.outSize;
   this.hmacStrength = this.constructor.hmacStrength;
+  this.padLength = this.constructor.padLength / 8;
   this.endian = 'big';
 
   this._delta8 = this.blockSize / 8;
@@ -52,8 +53,8 @@ BlockHash.prototype.digest = function digest(enc) {
 BlockHash.prototype._pad = function pad() {
   var len = this.pendingTotal;
   var bytes = this._delta8;
-  var k = bytes - ((len + 8) % bytes);
-  var res = new Array(k + 8);
+  var k = bytes - ((len + this.padLength) % bytes);
+  var res = new Array(k + this.padLength);
   res[0] = 0x80;
   for (var i = 1; i < k; i++)
     res[i] = 0;
@@ -61,6 +62,9 @@ BlockHash.prototype._pad = function pad() {
   // Append length
   len <<= 3;
   if (this.endian === 'big') {
+    for (var t = 8; t < this.padLength; t++)
+      res[i++] = 0;
+
     res[i++] = 0;
     res[i++] = 0;
     res[i++] = 0;
@@ -78,7 +82,10 @@ BlockHash.prototype._pad = function pad() {
     res[i++] = 0;
     res[i++] = 0;
     res[i++] = 0;
+
+    for (var t = 8; t < this.padLength; t++)
+      res[i++] = 0;
   }
 
   return res;
-}
+};
diff --git a/lib/hash/ripemd.js b/lib/hash/ripemd.js
index 54aed01..8eb28f4 100644
--- a/lib/hash/ripemd.js
+++ b/lib/hash/ripemd.js
@@ -22,6 +22,7 @@ exports.ripemd160 = RIPEMD160;
 RIPEMD160.blockSize = 512;
 RIPEMD160.outSize = 160;
 RIPEMD160.hmacStrength = 192;
+RIPEMD160.padLength = 64;
 
 RIPEMD160.prototype._update = function update(msg, start) {
   var A = this.h[0];
diff --git a/lib/hash/sha.js b/lib/hash/sha.js
index b03e299..affe767 100644
--- a/lib/hash/sha.js
+++ b/lib/hash/sha.js
@@ -7,6 +7,7 @@ var rotl32 = utils.rotl32;
 var sum32 = utils.sum32;
 var sum32_4 = utils.sum32_4;
 var sum32_5 = utils.sum32_5;
+var Num64 = utils.Num64;
 var BlockHash = hash.common.BlockHash;
 
 var sha256_K = [
@@ -28,6 +29,52 @@ var sha256_K = [
   0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
 ];
 
+var sha512_K = [
+  [ 0x428a2f98, 0xd728ae22], [ 0x71374491, 0x23ef65cd ],
+  [ 0xb5c0fbcf, 0xec4d3b2f], [ 0xe9b5dba5, 0x8189dbbc ],
+  [ 0x3956c25b, 0xf348b538], [ 0x59f111f1, 0xb605d019 ],
+  [ 0x923f82a4, 0xaf194f9b], [ 0xab1c5ed5, 0xda6d8118 ],
+  [ 0xd807aa98, 0xa3030242], [ 0x12835b01, 0x45706fbe ],
+  [ 0x243185be, 0x4ee4b28c], [ 0x550c7dc3, 0xd5ffb4e2 ],
+  [ 0x72be5d74, 0xf27b896f], [ 0x80deb1fe, 0x3b1696b1 ],
+  [ 0x9bdc06a7, 0x25c71235], [ 0xc19bf174, 0xcf692694 ],
+  [ 0xe49b69c1, 0x9ef14ad2], [ 0xefbe4786, 0x384f25e3 ],
+  [ 0x0fc19dc6, 0x8b8cd5b5], [ 0x240ca1cc, 0x77ac9c65 ],
+  [ 0x2de92c6f, 0x592b0275], [ 0x4a7484aa, 0x6ea6e483 ],
+  [ 0x5cb0a9dc, 0xbd41fbd4], [ 0x76f988da, 0x831153b5 ],
+  [ 0x983e5152, 0xee66dfab], [ 0xa831c66d, 0x2db43210 ],
+  [ 0xb00327c8, 0x98fb213f], [ 0xbf597fc7, 0xbeef0ee4 ],
+  [ 0xc6e00bf3, 0x3da88fc2], [ 0xd5a79147, 0x930aa725 ],
+  [ 0x06ca6351, 0xe003826f], [ 0x14292967, 0x0a0e6e70 ],
+  [ 0x27b70a85, 0x46d22ffc], [ 0x2e1b2138, 0x5c26c926 ],
+  [ 0x4d2c6dfc, 0x5ac42aed], [ 0x53380d13, 0x9d95b3df ],
+  [ 0x650a7354, 0x8baf63de], [ 0x766a0abb, 0x3c77b2a8 ],
+  [ 0x81c2c92e, 0x47edaee6], [ 0x92722c85, 0x1482353b ],
+  [ 0xa2bfe8a1, 0x4cf10364], [ 0xa81a664b, 0xbc423001 ],
+  [ 0xc24b8b70, 0xd0f89791], [ 0xc76c51a3, 0x0654be30 ],
+  [ 0xd192e819, 0xd6ef5218], [ 0xd6990624, 0x5565a910 ],
+  [ 0xf40e3585, 0x5771202a], [ 0x106aa070, 0x32bbd1b8 ],
+  [ 0x19a4c116, 0xb8d2d0c8], [ 0x1e376c08, 0x5141ab53 ],
+  [ 0x2748774c, 0xdf8eeb99], [ 0x34b0bcb5, 0xe19b48a8 ],
+  [ 0x391c0cb3, 0xc5c95a63], [ 0x4ed8aa4a, 0xe3418acb ],
+  [ 0x5b9cca4f, 0x7763e373], [ 0x682e6ff3, 0xd6b2b8a3 ],
+  [ 0x748f82ee, 0x5defb2fc], [ 0x78a5636f, 0x43172f60 ],
+  [ 0x84c87814, 0xa1f0ab72], [ 0x8cc70208, 0x1a6439ec ],
+  [ 0x90befffa, 0x23631e28], [ 0xa4506ceb, 0xde82bde9 ],
+  [ 0xbef9a3f7, 0xb2c67915], [ 0xc67178f2, 0xe372532b ],
+  [ 0xca273ece, 0xea26619c], [ 0xd186b8c7, 0x21c0c207 ],
+  [ 0xeada7dd6, 0xcde0eb1e], [ 0xf57d4f7f, 0xee6ed178 ],
+  [ 0x06f067aa, 0x72176fba], [ 0x0a637dc5, 0xa2c898a6 ],
+  [ 0x113f9804, 0xbef90dae], [ 0x1b710b35, 0x131c471b ],
+  [ 0x28db77f5, 0x23047d84], [ 0x32caab7b, 0x40c72493 ],
+  [ 0x3c9ebe0a, 0x15c9bebc], [ 0x431d67c4, 0x9c100d4c ],
+  [ 0x4cc5d4be, 0xcb3e42b6], [ 0x597f299c, 0xfc657e2a ],
+  [ 0x5fcb6fab, 0x3ad6faec], [ 0x6c44198c, 0x4a475817 ]
+];
+sha512_K = sha512_K.map(function(pair) {
+  return new Num64(pair[0], pair[1]);
+});
+
 var sha1_K = [
   0x5A827999, 0x6ED9EBA1,
   0x8F1BBCDC, 0xCA62C1D6
@@ -49,6 +96,7 @@ exports.sha256 = SHA256;
 SHA256.blockSize = 512;
 SHA256.outSize = 256;
 SHA256.hmacStrength = 192;
+SHA256.padLength = 64;
 
 SHA256.prototype._update = function _update(msg, start) {
   var W = this.W;
@@ -112,6 +160,7 @@ exports.sha224 = SHA224;
 SHA224.blockSize = 512;
 SHA224.outSize = 224;
 SHA224.hmacStrength = 192;
+SHA224.padLength = 64;
 
 SHA224.prototype._digest = function digest(enc) {
   // Just truncate output
@@ -121,6 +170,108 @@ SHA224.prototype._digest = function digest(enc) {
     return utils.split32(this.h.slice(0, 7), 'big');
 };
 
+function SHA512() {
+  if (!(this instanceof SHA512))
+    return new SHA512();
+
+  BlockHash.call(this);
+  this.h = [ new Num64(0x6a09e667, 0xf3bcc908),
+             new Num64(0xbb67ae85, 0x84caa73b),
+             new Num64(0x3c6ef372, 0xfe94f82b),
+             new Num64(0xa54ff53a, 0x5f1d36f1),
+             new Num64(0x510e527f, 0xade682d1),
+             new Num64(0x9b05688c, 0x2b3e6c1f),
+             new Num64(0x1f83d9ab, 0xfb41bd6b),
+             new Num64(0x5be0cd19, 0x137e2179) ];
+  this.k = sha512_K;
+  this.W = new Array(80);
+}
+utils.inherits(SHA512, BlockHash);
+exports.sha512 = SHA512;
+
+SHA512.blockSize = 1024;
+SHA512.outSize = 512;
+SHA512.hmacStrength = 192;
+SHA512.padLength = 128;
+
+SHA512.prototype._update = function _update(msg, start) {
+  var W = this.W;
+
+  // 32 x 32bit words
+  for (var i = 0; i < 16; i++)
+    W[i] = new Num64(msg[start + 2 * i], msg[start + 2 * i + 1]);
+  for (; i < W.length; i++)
+    W[i] = g1_512(W[i - 2]).sum3(W[i - 7], g0_512(W[i - 15]), W[i - 16]);
+
+  var a = this.h[0];
+  var b = this.h[1];
+  var c = this.h[2];
+  var d = this.h[3];
+  var e = this.h[4];
+  var f = this.h[5];
+  var g = this.h[6];
+  var h = this.h[7];
+
+  assert(this.k.length === W.length);
+  for (var i = 0; i < W.length; i++) {
+    var T1 = h.sum4(s1_512(e), ch64(e, f, g), this.k[i], W[i]);
+    var T2 = s0_512(a).sum(maj64(a, b, c));
+    h = g;
+    g = f;
+    f = e;
+    e = d.sum(T1);
+    d = c;
+    c = b;
+    b = a;
+    a = T1.sum(T2);
+  }
+
+  this.h[0].isum(a);
+  this.h[1].isum(b);
+  this.h[2].isum(c);
+  this.h[3].isum(d);
+  this.h[4].isum(e);
+  this.h[5].isum(f);
+  this.h[6].isum(g);
+  this.h[7].isum(h);
+};
+
+SHA512.prototype._digest = function digest(enc) {
+  if (enc === 'hex')
+    return utils.toHex64(this.h, 'big');
+  else
+    return utils.split64(this.h, 'big');
+};
+
+function SHA384() {
+  if (!(this instanceof SHA384))
+    return new SHA384();
+
+  SHA512.call(this);
+  this.h = [ new Num64(0xcbbb9d5d, 0xc1059ed8),
+             new Num64(0x629a292a, 0x367cd507),
+             new Num64(0x9159015a, 0x3070dd17),
+             new Num64(0x152fecd8, 0xf70e5939),
+             new Num64(0x67332667, 0xffc00b31),
+             new Num64(0x8eb44a87, 0x68581511),
+             new Num64(0xdb0c2e0d, 0x64f98fa7),
+             new Num64(0x47b5481d, 0xbefa4fa4) ];
+}
+utils.inherits(SHA384, SHA512);
+exports.sha384 = SHA384;
+
+SHA384.blockSize = 1024;
+SHA384.outSize = 384;
+SHA384.hmacStrength = 192;
+SHA384.padLength = 128;
+
+SHA384.prototype._digest = function digest(enc) {
+  if (enc === 'hex')
+    return utils.toHex64(this.h.slice(0, 6), 'big');
+  else
+    return utils.split64(this.h.slice(0, 6), 'big');
+};
+
 function SHA1() {
   if (!(this instanceof SHA1))
     return new SHA1();
@@ -137,6 +288,7 @@ exports.sha1 = SHA1;
 SHA1.blockSize = 512;
 SHA1.outSize = 160;
 SHA1.hmacStrength = 80;
+SHA1.padLength = 64;
 
 SHA1.prototype._update = function _update(msg, start) {
   var W = this.W;
@@ -209,7 +361,37 @@ function ft_1(s, x, y, z) {
   if (s === 0)
     return ch32(x, y, z);
   if (s === 1 || s === 3)
-    return p32(x, y, z)
+    return p32(x, y, z);
   if (s === 2)
-    return maj32(x, y, z)
+    return maj32(x, y, z);
+}
+
+function ch64(x, y, z) {
+  return new Num64(
+    (x.hi & y.hi) ^ ((~x.hi) & z.hi),
+    (x.lo & y.lo) ^ ((~x.lo) & z.lo)
+  );
+}
+
+function maj64(x, y, z) {
+  return new Num64(
+    (x.hi & y.hi) ^ (x.hi & z.hi) ^ (y.hi & z.hi),
+    (x.lo & y.lo) ^ (x.lo & z.lo) ^ (y.lo & z.lo)
+  );
+}
+
+function s0_512(x, y, z) {
+  return x.rotr(28).xor2(x.rotr(34), x.rotr(39));
+}
+
+function s1_512(x, y, z) {
+  return x.rotr(14).xor2(x.rotr(18), x.rotr(41));
+}
+
+function g0_512(x, y, z) {
+  return x.rotr(1).xor2(x.rotr(8), x.shr(7));
+}
+
+function g1_512(x, y, z) {
+  return x.rotr(19).xor2(x.rotr(61), x.shr(6));
 }
diff --git a/lib/hash/utils.js b/lib/hash/utils.js
index b077716..33ef3b9 100644
--- a/lib/hash/utils.js
+++ b/lib/hash/utils.js
@@ -20,7 +20,7 @@ function toArray(msg, enc) {
       }
     } else if (enc === 'hex') {
       msg = msg.replace(/[^a-z0-9]+/ig, '');
-      if (msg.length % 2 != 0)
+      if (msg.length % 2 !== 0)
         msg = '0' + msg;
       for (var i = 0; i < msg.length; i += 2)
         res.push(parseInt(msg[i] + msg[i + 1], 16));
@@ -41,24 +41,46 @@ function toHex(msg) {
 }
 utils.toHex = toHex;
 
+function htonl(w) {
+  var res = (w >>> 24) |
+            ((w >>> 8) & 0xff00) |
+            ((w << 8) & 0xff0000) |
+            ((w & 0xff) << 24);
+  if (res < 0)
+    res += 0x100000000;
+  return res;
+}
+utils.htonl = htonl;
+
 function toHex32(msg, endian) {
   var res = '';
   for (var i = 0; i < msg.length; i++) {
     var w = msg[i];
-    if (endian === 'little') {
-      w = (w >>> 24) |
-          ((w >>> 8) & 0xff00) |
-          ((w << 8) & 0xff0000) |
-          ((w & 0xff) << 24);
-      if (w < 0)
-        w += 0x100000000;
-    }
+    if (endian === 'little')
+      w = htonl(w);
     res += zero8(w.toString(16));
   }
   return res;
 }
 utils.toHex32 = toHex32;
 
+function toHex64(msg, endian) {
+  var res = '';
+  for (var i = 0; i < msg.length; i++) {
+    var hi = msg[i].hi;
+    var lo = msg[i].lo;
+    if (endian === 'little') {
+      hi = htonl(hi);
+      lo = htonl(lo);
+      res += zero8(lo.toString(16)) + zero8(hi.toString(16));
+    } else {
+      res += zero8(hi.toString(16)) + zero8(lo.toString(16));
+    }
+  }
+  return res;
+}
+utils.toHex64 = toHex64;
+
 function zero2(word) {
   if (word.length === 1)
     return '0' + word;
@@ -125,6 +147,35 @@ function split32(msg, endian) {
 }
 utils.split32 = split32;
 
+function split64(msg, endian) {
+  var res = new Array(msg.length * 8);
+  for (var i = 0, k = 0; i < msg.length; i++, k += 8) {
+    var hi = msg[i].hi;
+    var lo = msg[i].lo;
+    if (endian === 'big') {
+      res[k] = hi >>> 24;
+      res[k + 1] = (hi >>> 16) & 0xff;
+      res[k + 2] = (hi >>> 8) & 0xff;
+      res[k + 3] = hi & 0xff;
+      res[k + 4] = lo >>> 24;
+      res[k + 5] = (lo >>> 16) & 0xff;
+      res[k + 6] = (lo >>> 8) & 0xff;
+      res[k + 7] = lo & 0xff;
+    } else {
+      res[k + 7] = hi >>> 24;
+      res[k + 6] = (hi >>> 16) & 0xff;
+      res[k + 5] = (hi >>> 8) & 0xff;
+      res[k + 4] = hi & 0xff;
+      res[k + 3] = lo >>> 24;
+      res[k + 2] = (lo >>> 16) & 0xff;
+      res[k + 1] = (lo >>> 8) & 0xff;
+      res[k] = lo & 0xff;
+    }
+  }
+  return res;
+}
+utils.split64 = split64;
+
 function rotr32(w, b) {
   return (w >>> b) | (w << (32 - b));
 }
@@ -174,3 +225,81 @@ function assert(cond, msg) {
 utils.assert = assert;
 
 utils.inherits = inherits;
+
+function Num64(hi, lo) {
+  if (hi < 0)
+    this.hi = hi + 0x100000000;
+  else
+    this.hi = hi;
+  if (lo < 0)
+    this.lo = lo + 0x100000000;
+  else
+    this.lo = lo;
+}
+utils.Num64 = Num64;
+
+Num64.prototype.inspect = function inspect() {
+  return '0x' + zero8(this.hi.toString(16)) + zero8(this.lo.toString(16));
+};
+
+Num64.prototype.clone = function clone() {
+  return new Num64(this.hi, this.lo);
+};
+
+Num64.prototype.xor2 = function xor2(a, b) {
+  return new Num64(
+    a.hi ^ b.hi ^ this.hi,
+    a.lo ^ b.lo ^ this.lo
+  );
+};
+
+Num64.prototype.sum3 = function sum3(a, b, c) {
+  return this.clone().isum(a).isum(b).isum(c);
+};
+
+Num64.prototype.sum4 = function sum4(a, b, c, d) {
+  return this.clone().isum(a).isum(b).isum(c).isum(d);
+};
+
+Num64.prototype.rotr = function rotr(num) {
+  if (num >= 32)
+    return new Num64(this.lo, this.hi).rotr(num - 32);
+
+  // num < 32
+  var mask = (1 << num) - 1;
+  var rnum = 32 - num;
+  return new Num64(
+    ((this.lo & mask) << rnum) | (this.hi >>> num),
+    ((this.hi & mask) << rnum) | (this.lo >>> num)
+  );
+};
+
+Num64.prototype.shr = function shr(num) {
+  if (num >= 32)
+    return new Num64(0, this.hi).shr(num - 32);
+
+  // num < 32
+  var mask = (1 << num) - 1;
+  return new Num64(
+    this.hi >>> num,
+    ((this.hi & mask) << (32 - num)) | (this.lo >>> num)
+  );
+};
+
+Num64.prototype.isum = function isum(num) {
+  var lo = this.lo + num.lo;
+  var hi = (((lo / 0x100000000) | 0) + this.hi + num.hi) & 0xffffffff;
+
+  this.hi = hi;
+  this.lo = lo & 0xffffffff;
+  if (this.hi < 0)
+    this.hi += 0x100000000;
+  if (this.lo < 0)
+    this.lo += 0x100000000;
+
+  return this;
+};
+
+Num64.prototype.sum = function sum(num) {
+  return this.clone().isum(num);
+};
diff --git a/test/hash-test.js b/test/hash-test.js
index bec2d6a..97347a2 100644
--- a/test/hash-test.js
+++ b/test/hash-test.js
@@ -2,11 +2,28 @@ var assert = require('assert');
 var hash = require('../');
 
 describe('Hash', function() {
+  function test(fn, cases) {
+    for (var i = 0; i < cases.length; i++) {
+      var msg = cases[i][0];
+      var res = cases[i][1];
+      var enc = cases[i][2];
+
+      var dgst = fn().update(msg, enc).digest('hex');
+      assert.equal(dgst, res);
+
+      // Split message
+      var dgst = fn().update(msg.slice(0, 2), enc)
+                     .update(msg.slice(2), enc)
+                     .digest('hex');
+      assert.equal(dgst, res);
+    }
+  }
+
   it('should support sha256', function() {
     assert.equal(hash.sha256.blockSize, 512);
     assert.equal(hash.sha256.outSize, 256);
 
-    var test = [
+    test(hash.sha256, [
       [ 'abc',
         'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad' ],
       [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
@@ -14,29 +31,14 @@ describe('Hash', function() {
       [ 'deadbeef',
         '5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953',
         'hex' ],
-    ];
-    for (var i = 0; i < test.length; i++) {
-      var msg = test[i][0];
-      var res = test[i][1];
-      var enc = test[i][2];
-
-      var dgst = hash.sha256().update(msg, enc).digest('hex');
-      assert.equal(dgst, res);
-
-      // Split message
-      var dgst = hash.sha256()
-                     .update(msg.slice(0, 2), enc)
-                     .update(msg.slice(2), enc)
-                     .digest('hex');
-      assert.equal(dgst, res);
-    }
+    ]);
   });
 
   it('should support sha224', function() {
     assert.equal(hash.sha224.blockSize, 512);
     assert.equal(hash.sha224.outSize, 224);
 
-    var test = [
+    test(hash.sha224, [
       [ 'abc',
         '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7' ],
       [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
@@ -44,29 +46,14 @@ describe('Hash', function() {
       [ 'deadbeef',
         '55b9eee5f60cc362ddc07676f620372611e22272f60fdbec94f243f8',
         'hex' ],
-    ];
-    for (var i = 0; i < test.length; i++) {
-      var msg = test[i][0];
-      var res = test[i][1];
-      var enc = test[i][2];
-
-      var dgst = hash.sha224().update(msg, enc).digest('hex');
-      assert.equal(dgst, res);
-
-      // Split message
-      var dgst = hash.sha224()
-                     .update(msg.slice(0, 2), enc)
-                     .update(msg.slice(2), enc)
-                     .digest('hex');
-      assert.equal(dgst, res);
-    }
+    ]);
   });
 
   it('should support ripemd160', function() {
     assert.equal(hash.ripemd160.blockSize, 512);
     assert.equal(hash.ripemd160.outSize, 160);
 
-    var test = [
+    test(hash.ripemd160, [
       [ '', '9c1185a5c5e9fc54612808977ee8f548b2258d31'],
       [ 'abc',
         '8eb208f7e05d987a9b044a8e98c6b087f15a0bfc' ],
@@ -76,29 +63,14 @@ describe('Hash', function() {
         '12a053384a9c0c88e405a06c27dcf49ada62eb2b' ],
       [ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
         'b0e20b6e3116640286ed3a87a5713079b21f5189' ],
-    ];
-    for (var i = 0; i < test.length; i++) {
-      var msg = test[i][0];
-      var res = test[i][1];
-      var enc = test[i][2];
-
-      var dgst = hash.ripemd160().update(msg, enc).digest('hex');
-      assert.equal(dgst, res);
-
-      // Split message on odd boundary
-      var dgst = hash.ripemd160()
-                     .update(msg.slice(0, 3), enc)
-                     .update(msg.slice(3), enc)
-                     .digest('hex');
-      assert.equal(dgst, res);
-    }
+    ]);
   });
 
   it('should support sha1', function() {
     assert.equal(hash.sha1.blockSize, 512);
     assert.equal(hash.sha1.outSize, 160);
 
-    var test = [
+    test(hash.sha1, [
       [ '',
         'da39a3ee5e6b4b0d3255bfef95601890afd80709' ],
       [ 'abc',
@@ -108,21 +80,40 @@ describe('Hash', function() {
       [ 'deadbeef',
         'd78f8bb992a56a597f6c7a1fb918bb78271367eb',
         'hex' ],
-    ];
-    for (var i = 0; i < test.length; i++) {
-      var msg = test[i][0];
-      var res = test[i][1];
-      var enc = test[i][2];
+    ]);
+  });
 
-      var dgst = hash.sha1().update(msg, enc).digest('hex');
-      assert.equal(dgst, res);
+  it('should support sha512', function() {
+    assert.equal(hash.sha512.blockSize, 1024);
+    assert.equal(hash.sha512.outSize, 512);
 
-      // Split message
-      var dgst = hash.sha1()
-                     .update(msg.slice(0, 2), enc)
-                     .update(msg.slice(2), enc)
-                     .digest('hex');
-      assert.equal(dgst, res);
-    }
+    test(hash.sha512, [
+      [ 'abc',
+        'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a' +
+            '2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f'
+      ],
+      [ 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn' +
+            'hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',
+        '8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018' +
+            '501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909'
+      ]
+    ]);
+  });
+
+  it('should support sha384', function() {
+    assert.equal(hash.sha384.blockSize, 1024);
+    assert.equal(hash.sha384.outSize, 384);
+
+    test(hash.sha384, [
+      [ 'abc',
+        'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed' +
+            '8086072ba1e7cc2358baeca134c825a7'
+      ],
+      [ 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn' +
+            'hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',
+        '09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712' +
+            'fcc7c71a557e2db966c3e9fa91746039'
+      ]
+    ]);
   });
 });

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