Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

feat: add many sha3 functions #24

Merged
merged 2 commits into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -51,6 +52,7 @@
"Harlan T Wood <[email protected]>",
"Juan Benet <[email protected]>",
"Richard Littauer <[email protected]>",
"Henrique Dias <[email protected]>",
"npm-to-cdn-bot (by Forbes Lindesay) <[email protected]>"
]
}
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
45 changes: 45 additions & 0 deletions src/sha3.js
Original file line number Diff line number Diff line change
@@ -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
}
92 changes: 33 additions & 59 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down