From d318fdb5577600b2adf83f71dfac02d0177a723c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BA=D0=BE=D0=B2=D0=BE=D1=80=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=90=D0=BD=D0=B4=D1=80?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Sun, 31 May 2020 15:31:06 +0300 Subject: [PATCH 1/3] Implement sha512-256 and sha512-224 --- index.js | 2 ++ sha512-224.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ sha512-256.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 sha512-224.js create mode 100644 sha512-256.js diff --git a/index.js b/index.js index 87cdf49..8dee2ec 100644 --- a/index.js +++ b/index.js @@ -13,3 +13,5 @@ exports.sha224 = require('./sha224') exports.sha256 = require('./sha256') exports.sha384 = require('./sha384') exports.sha512 = require('./sha512') +exports['sha512-224'] = require('./sha512-224') +exports['sha512-256'] = require('./sha512-256') diff --git a/sha512-224.js b/sha512-224.js new file mode 100644 index 0000000..bea7d37 --- /dev/null +++ b/sha512-224.js @@ -0,0 +1,53 @@ +var inherits = require('inherits') +var Sha512 = require('./sha512') +var Hash = require('./hash') +var Buffer = require('safe-buffer').Buffer + +var W = new Array(160) + +function Sha512h256 () { + this.init() + this._w = W + + Hash.call(this, 128, 112) +} + +inherits(Sha512h256, Sha512) + +Sha512h256.prototype.init = function () { + this._ah = 0x8c3d37c8 + this._bh = 0x73e19966 + this._ch = 0x1dfab7ae + this._dh = 0x679dd514 + this._eh = 0x0f6d2b69 + this._fh = 0x77e36f73 + this._gh = 0x3f9d85a8 + this._hh = 0x1112e6ad + + this._al = 0x19544da2 + this._bl = 0x89dcd4d6 + this._cl = 0x32ff9c82 + this._dl = 0x582f9fcf + this._el = 0x7bd44da8 + this._fl = 0x04c48942 + this._gl = 0x6a1d36c8 + this._hl = 0x91d692a1 + + return this +} + +Sha512h256.prototype._hash = function () { + var H = Buffer.allocUnsafe(28) + + H.writeInt32BE(this._ah, 0) + H.writeInt32BE(this._al, 4) + H.writeInt32BE(this._bh, 8) + H.writeInt32BE(this._bl, 12) + H.writeInt32BE(this._ch, 16) + H.writeInt32BE(this._cl, 20) + H.writeInt32BE(this._dh, 24) + + return H +} + +module.exports = Sha512h256 diff --git a/sha512-256.js b/sha512-256.js new file mode 100644 index 0000000..9c92a76 --- /dev/null +++ b/sha512-256.js @@ -0,0 +1,54 @@ +var inherits = require('inherits') +var Sha512 = require('./sha512') +var Hash = require('./hash') +var Buffer = require('safe-buffer').Buffer + +var W = new Array(160) + +function Sha512h256 () { + this.init() + this._w = W + + Hash.call(this, 128, 112) +} + +inherits(Sha512h256, Sha512) + +Sha512h256.prototype.init = function () { + this._ah = 0x22312194 + this._bh = 0x9f555fa3 + this._ch = 0x2393b86b + this._dh = 0x96387719 + this._eh = 0x96283ee2 + this._fh = 0xbe5e1e25 + this._gh = 0x2b0199fc + this._hh = 0x0eb72ddc + + this._al = 0xfc2bf72c + this._bl = 0xc84c64c2 + this._cl = 0x6f53b151 + this._dl = 0x5940eabd + this._el = 0xa88effe3 + this._fl = 0x53863992 + this._gl = 0x2c85b8aa + this._hl = 0x81c52ca2 + + return this +} + +Sha512h256.prototype._hash = function () { + var H = Buffer.allocUnsafe(32) + + H.writeInt32BE(this._ah, 0) + H.writeInt32BE(this._al, 4) + H.writeInt32BE(this._bh, 8) + H.writeInt32BE(this._bl, 12) + H.writeInt32BE(this._ch, 16) + H.writeInt32BE(this._cl, 20) + H.writeInt32BE(this._dh, 24) + H.writeInt32BE(this._dl, 28) + + return H +} + +module.exports = Sha512h256 From 55e9196c0eee137b3dc2874d3008b67f16a5af8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BA=D0=BE=D0=B2=D0=BE=D1=80=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=90=D0=BD=D0=B4=D1=80?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Sun, 31 May 2020 15:37:56 +0300 Subject: [PATCH 2/3] Test on newer Node.js versions, too Older don't have sha512-256 and sha512-224, but they should get tested. --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index 0b606eb..c8ad14c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,10 @@ node_js: - "5" - "6" - "7" + - "8" + - "10" + - "12" + - "14" env: matrix: - TEST_SUITE=unit From c52c81b52f171347f91c3ce319c9a61e45d7a02c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BA=D0=BE=D0=B2=D0=BE=D1=80=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=90=D0=BD=D0=B4=D1=80?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Sun, 31 May 2020 15:20:46 +0300 Subject: [PATCH 3/3] test: test that all algos match Node.js --- test/test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test.js b/test/test.js index dac8580..26da03f 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,7 @@ var crypto = require('crypto') var tape = require('tape') var Sha1 = require('../').sha1 +var sha = require('../') var inputs = [ ['', 'ascii'], @@ -25,6 +26,21 @@ tape("hash is the same as node's crypto", function (t) { t.end() }) +tape("hash is the same as node's crypto for all algos provided by node", function (t) { + var hashes = crypto.getHashes() + Object.keys(sha).forEach(function (alg) { + if (hashes.indexOf(alg) === -1) return // skip unsupported by current Node.js version + inputs.forEach(function (v) { + var a = new sha[alg]().update(v[0], v[1]).digest('hex') + var e = crypto.createHash(alg).update(v[0], v[1]).digest('hex') + console.log(alg, a, '==', e) + t.equal(a, e) + }) + }) + + t.end() +}) + tape('call update multiple times', function (t) { inputs.forEach(function (v) { var hash = new Sha1()