[Pkg-javascript-commits] [node-hash.js] 11/29: hash: add sha1

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 19f3ed86956ffcdc06f7cc0a196dfdd3b7fba7f0
Author: Terin Stock <terinjokes at gmail.com>
Date:   Fri May 9 21:51:44 2014 -0400

    hash: add sha1
    
    Signed-off-by: Fedor Indutny <fedor at indutny.com>
---
 lib/hash.js       |  1 +
 lib/hash/sha.js   | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/hash-test.js | 32 ++++++++++++++++++++++++
 3 files changed, 106 insertions(+)

diff --git a/lib/hash.js b/lib/hash.js
index c6a62da..56c611b 100644
--- a/lib/hash.js
+++ b/lib/hash.js
@@ -7,6 +7,7 @@ hash.ripemd = require('./hash/ripemd');
 hash.hmac = require('./hash/hmac');
 
 // Proxy hash functions to the main object
+hash.sha1 = hash.sha.sha1;
 hash.sha256 = hash.sha.sha256;
 hash.sha224 = hash.sha.sha224;
 hash.ripemd160 = hash.ripemd.ripemd160;
diff --git a/lib/hash/sha.js b/lib/hash/sha.js
index 7b9b673..e2553ea 100644
--- a/lib/hash/sha.js
+++ b/lib/hash/sha.js
@@ -28,6 +28,11 @@ var sha256_K = [
   0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
 ];
 
+var sha1_K = [
+  0x5A827999, 0x6ED9EBA1,
+  0x8F1BBCDC, 0xCA62C1D6
+];
+
 function SHA256() {
   if (!(this instanceof SHA256))
     return new SHA256();
@@ -114,6 +119,61 @@ SHA224.prototype._digest = function digest(enc) {
     return utils.split32(this.h.slice(0, 7), 'big');
 };
 
+function SHA1() {
+  if (!(this instanceof SHA1))
+    return new SHA1();
+
+  BlockHash.call(this);
+  this.h = [ 0x67452301, 0xefcdab89, 0x98badcfe,
+             0x10325476, 0xc3d2e1f0 ];
+}
+
+utils.inherits(SHA1, BlockHash);
+exports.sha1 = SHA1;
+
+SHA1.blockSize = 512;
+SHA1.outSize = 160;
+SHA1.hmacStrength = 80;
+
+SHA1.prototype._update = function _update(msg) {
+  var W = Array(80);
+
+  for (var i = 0; i < 16; i++)
+    W[i] = msg[i];
+
+  for(; i < W.length; i++)
+    W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
+
+  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];
+
+  for (var i = 0; i < W.length; i++) {
+    var s = ~~(i / 20);
+    var t = sum32_5(rotl32(a, 5), ft_1(s, b, c, d), e, W[i], sha1_K[s]);
+    e = d;
+    d = c;
+    c = rotl32(b, 30);
+    b = a;
+    a = t;
+  }
+
+  this.h[0] = sum32(this.h[0], a);
+  this.h[1] = sum32(this.h[1], b);
+  this.h[2] = sum32(this.h[2], c);
+  this.h[3] = sum32(this.h[3], d);
+  this.h[4] = sum32(this.h[4], e);
+};
+
+SHA1.prototype._digest = function digest(enc) {
+  if (enc === 'hex')
+    return utils.toHex32(this.h, 'big');
+  else
+    return utils.split32(this.h, 'big');
+};
+
 function ch32(x, y, z) {
   return (x & y) ^ ((~x) & z);
 }
@@ -122,6 +182,10 @@ function maj32(x, y, z) {
   return (x & y) ^ (x & z) ^ (y & z);
 }
 
+function p32(x, y, z) {
+  return x ^ y ^ z;
+}
+
 function s0_256(x) {
   return rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22);
 }
@@ -137,3 +201,12 @@ function g0_256(x) {
 function g1_256(x) {
   return rotr32(x, 17) ^ rotr32(x, 19) ^ (x >>> 10);
 }
+
+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)
+  if (s === 2)
+    return maj32(x, y, z)
+}
diff --git a/test/hash-test.js b/test/hash-test.js
index 35ac7b3..bec2d6a 100644
--- a/test/hash-test.js
+++ b/test/hash-test.js
@@ -93,4 +93,36 @@ describe('Hash', function() {
       assert.equal(dgst, res);
     }
   });
+
+  it('should support sha1', function() {
+    assert.equal(hash.sha1.blockSize, 512);
+    assert.equal(hash.sha1.outSize, 160);
+
+    var test = [
+      [ '',
+        'da39a3ee5e6b4b0d3255bfef95601890afd80709' ],
+      [ 'abc',
+        'a9993e364706816aba3e25717850c26c9cd0d89d' ],
+      [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
+        '84983e441c3bd26ebaae4aa1f95129e5e54670f1' ],
+      [ '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);
+
+      // Split message
+      var dgst = hash.sha1()
+                     .update(msg.slice(0, 2), enc)
+                     .update(msg.slice(2), enc)
+                     .digest('hex');
+      assert.equal(dgst, res);
+    }
+  });
 });

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