From 91145d308919143ad6d940308881db66cdeee9ac Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 2 Jan 2018 12:39:57 +0000 Subject: [PATCH] feat: add many sha3 functions (#24) * feat: add many sha3 functions * tests: create tests table --- .gitignore | 2 + package.json | 12 +++--- src/index.js | 3 +- src/sha3.js | 45 +++++++++++++++++++++++ test/index.spec.js | 92 +++++++++++++++++----------------------------- 5 files changed, 89 insertions(+), 65 deletions(-) create mode 100644 src/sha3.js diff --git a/.gitignore b/.gitignore index 5b1fd19..6b49360 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,8 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules +yarn.lock +package-lock.json dist lib \ No newline at end of file diff --git a/package.json b/package.json index b02a563..e2c7f82 100644 --- a/package.json +++ b/package.json @@ -33,14 +33,15 @@ "url": "https://github.com/multiformats/js-multihashing/issues" }, "dependencies": { - "blakejs": "^1.0.1", - "multihashes": "~0.4.4", + "blakejs": "^1.1.0", + "js-sha3": "^0.7.0", + "multihashes": "~0.4.12", "webcrypto": "~0.1.1" }, "devDependencies": { - "aegir": "^12.2.0", - "chai": "^3.5.0", - "dirty-chai": "^1.2.2", + "aegir": "^12.3.0", + "chai": "^4.1.2", + "dirty-chai": "^2.0.1", "pre-commit": "^1.2.2" }, "homepage": "https://github.com/multiformats/js-multihashing", @@ -51,6 +52,7 @@ "Harlan T Wood ", "Juan Benet ", "Richard Littauer ", + "Henrique Dias ", "npm-to-cdn-bot (by Forbes Lindesay) " ] } diff --git a/src/index.js b/src/index.js index 8c8b00f..ded12a6 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ const multihash = require('multihashes') const blake = require('./blake') +const sha3 = require('./sha3') const crypto = require('webcrypto') const mh = module.exports = Multihashing @@ -38,10 +39,10 @@ mh.functions = { 0x11: gsha1, 0x12: gsha2256, 0x13: gsha2512 - // 0x14: gsha3 // not implemented yet } blake.addFuncs(mh.functions) +sha3.addFuncs(mh.functions) function gsha1 () { return crypto.createHash('sha1') diff --git a/src/sha3.js b/src/sha3.js new file mode 100644 index 0000000..4f4b131 --- /dev/null +++ b/src/sha3.js @@ -0,0 +1,45 @@ +'use strict' + +const sha3 = require('js-sha3') + +const functions = { + 0x14: sha3.sha3_512, + 0x15: sha3.sha3_384, + 0x16: sha3.sha3_256, + 0x17: sha3.sha3_224, + 0x18: sha3.shake128, + 0x19: sha3.shake256, + 0x1A: sha3.keccak224, + 0x1B: sha3.keccak256, + 0x1C: sha3.keccak384, + 0x1D: sha3.keccak512 +} + +class Hasher { + constructor (hashFunc) { + this.hf = hashFunc + this.input = null + } + + update (buf) { + this.input = buf + return this + } + + digest () { + const input = this.input + return Buffer.from(this.hf(input)) + } +} + +function addFuncs (table) { + for (const code in functions) { + if (functions.hasOwnProperty(code)) { + table[code] = () => new Hasher(functions[code]) + } + } +} + +module.exports = { + addFuncs: addFuncs +} diff --git a/test/index.spec.js b/test/index.spec.js index 2002139..0bde2ee 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -7,66 +7,40 @@ chai.use(dirtyChai) const expect = chai.expect const multihashing = require('../src') -describe('multihashing', () => { - it('sha1', () => { - const buf = Buffer.from('beep boop') - - expect( - multihashing(buf, 'sha1') - ).to.be.eql( - Buffer.from('11147c8357577f51d4f0a8d393aa1aaafb28863d9421', 'hex') - ) - }) - - it('sha2-256', () => { - const buf = Buffer.from('beep boop') - - expect( - multihashing(buf, 'sha2-256') - ).to.be.eql( - Buffer.from('122090ea688e275d580567325032492b597bc77221c62493e76330b85ddda191ef7c', 'hex') - ) - }) - - it('sha2-512', () => { - const buf = Buffer.from('beep boop') - - expect( - multihashing(buf, 'sha2-512') - ).to.be.eql( - Buffer.from('134014f301f31be243f34c5668937883771fa381002f1aaa5f31b3f78e500b66ff2f4f8ea5e3c9f5a61bd073e2452c480484b02e030fb239315a2577f7ae156af177', 'hex') - ) - }) +const tests = { + 'sha1': [ + ['beep boop', '11147c8357577f51d4f0a8d393aa1aaafb28863d9421'] + ], + 'sha2-256': [ + ['beep boop', '122090ea688e275d580567325032492b597bc77221c62493e76330b85ddda191ef7c'] + ], + 'sha2-512': [ + ['beep boop', '134014f301f31be243f34c5668937883771fa381002f1aaa5f31b3f78e500b66ff2f4f8ea5e3c9f5a61bd073e2452c480484b02e030fb239315a2577f7ae156af177'] + ], + 'blake2b-512': [ + ['beep boop', 'c0e402400eac6255ba822373a0948122b8d295008419a8ab27842ee0d70eca39855621463c03ec75ac3610aacfdff89fa989d8d61fc00450148f289eb5b12ad1a954f659'] + ], + 'blake2b-160': [ + ['beep boop', '94e40214fe303247293e54e0a7ea48f9408ca68b36b08442'] + ], + 'blake2s-256': [ + ['beep boop', 'e0e402204542eaca484e4311def8af74b546edd7fceb49eeb3cdcfd8a4a72ed0dc81d4c0'] + ], + 'blake2s-40': [ + ['beep boop', 'c5e402059ada01bb57'] + ], +} - it('blake2b', () => { - const buf = Buffer.from('beep boop') - - expect( - multihashing(buf, 'blake2b-512') - ).to.be.eql( - Buffer.from('c0e402400eac6255ba822373a0948122b8d295008419a8ab27842ee0d70eca39855621463c03ec75ac3610aacfdff89fa989d8d61fc00450148f289eb5b12ad1a954f659', 'hex') - ) - expect( - multihashing(buf, 'blake2b-160') - ).to.be.eql( - Buffer.from('94e40214fe303247293e54e0a7ea48f9408ca68b36b08442', 'hex') - ) - }) - - it('blake2s', () => { - const buf = Buffer.from('beep boop') - - expect( - multihashing(buf, 'blake2s-256') - ).to.be.eql( - Buffer.from('e0e402204542eaca484e4311def8af74b546edd7fceb49eeb3cdcfd8a4a72ed0dc81d4c0', 'hex') - ) - expect( - multihashing(buf, 'blake2s-40') - ).to.be.eql( - Buffer.from('c5e402059ada01bb57', 'hex') - ) - }) +describe('multihashing', () => { + for (const algo in tests) { + it(algo, () => { + for (const test of tests[algo]) { + const input = Buffer.from(test[0]) + const output = Buffer.from(test[1], 'hex') + expect(multihashing(input, algo)).to.be.eql(output) + } + }) + } it('cuts the length', () => { const buf = Buffer.from('beep boop')