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

Commit 91145d3

Browse files
hacdiasdaviddias
authored andcommitted
feat: add many sha3 functions (#24)
* feat: add many sha3 functions * tests: create tests table
1 parent 0789414 commit 91145d3

File tree

5 files changed

+89
-65
lines changed

5 files changed

+89
-65
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ build/Release
2525
# Dependency directory
2626
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
2727
node_modules
28+
yarn.lock
29+
package-lock.json
2830

2931
dist
3032
lib

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
3333
"url": "https://github.com/multiformats/js-multihashing/issues"
3434
},
3535
"dependencies": {
36-
"blakejs": "^1.0.1",
37-
"multihashes": "~0.4.4",
36+
"blakejs": "^1.1.0",
37+
"js-sha3": "^0.7.0",
38+
"multihashes": "~0.4.12",
3839
"webcrypto": "~0.1.1"
3940
},
4041
"devDependencies": {
41-
"aegir": "^12.2.0",
42-
"chai": "^3.5.0",
43-
"dirty-chai": "^1.2.2",
42+
"aegir": "^12.3.0",
43+
"chai": "^4.1.2",
44+
"dirty-chai": "^2.0.1",
4445
"pre-commit": "^1.2.2"
4546
},
4647
"homepage": "https://github.com/multiformats/js-multihashing",
@@ -51,6 +52,7 @@
5152
"Harlan T Wood <harlantwood@users.noreply.github.com>",
5253
"Juan Benet <juan@benet.ai>",
5354
"Richard Littauer <richard.littauer@gmail.com>",
55+
"Henrique Dias <hacdias@gmail.com>",
5456
"npm-to-cdn-bot (by Forbes Lindesay) <npmcdn-to-unpkg-bot@users.noreply.github.com>"
5557
]
5658
}

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multihash = require('multihashes')
44
const blake = require('./blake')
5+
const sha3 = require('./sha3')
56
const crypto = require('webcrypto')
67

78
const mh = module.exports = Multihashing
@@ -38,10 +39,10 @@ mh.functions = {
3839
0x11: gsha1,
3940
0x12: gsha2256,
4041
0x13: gsha2512
41-
// 0x14: gsha3 // not implemented yet
4242
}
4343

4444
blake.addFuncs(mh.functions)
45+
sha3.addFuncs(mh.functions)
4546

4647
function gsha1 () {
4748
return crypto.createHash('sha1')

src/sha3.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
const sha3 = require('js-sha3')
4+
5+
const functions = {
6+
0x14: sha3.sha3_512,
7+
0x15: sha3.sha3_384,
8+
0x16: sha3.sha3_256,
9+
0x17: sha3.sha3_224,
10+
0x18: sha3.shake128,
11+
0x19: sha3.shake256,
12+
0x1A: sha3.keccak224,
13+
0x1B: sha3.keccak256,
14+
0x1C: sha3.keccak384,
15+
0x1D: sha3.keccak512
16+
}
17+
18+
class Hasher {
19+
constructor (hashFunc) {
20+
this.hf = hashFunc
21+
this.input = null
22+
}
23+
24+
update (buf) {
25+
this.input = buf
26+
return this
27+
}
28+
29+
digest () {
30+
const input = this.input
31+
return Buffer.from(this.hf(input))
32+
}
33+
}
34+
35+
function addFuncs (table) {
36+
for (const code in functions) {
37+
if (functions.hasOwnProperty(code)) {
38+
table[code] = () => new Hasher(functions[code])
39+
}
40+
}
41+
}
42+
43+
module.exports = {
44+
addFuncs: addFuncs
45+
}

test/index.spec.js

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,40 @@ chai.use(dirtyChai)
77
const expect = chai.expect
88
const multihashing = require('../src')
99

10-
describe('multihashing', () => {
11-
it('sha1', () => {
12-
const buf = Buffer.from('beep boop')
13-
14-
expect(
15-
multihashing(buf, 'sha1')
16-
).to.be.eql(
17-
Buffer.from('11147c8357577f51d4f0a8d393aa1aaafb28863d9421', 'hex')
18-
)
19-
})
20-
21-
it('sha2-256', () => {
22-
const buf = Buffer.from('beep boop')
23-
24-
expect(
25-
multihashing(buf, 'sha2-256')
26-
).to.be.eql(
27-
Buffer.from('122090ea688e275d580567325032492b597bc77221c62493e76330b85ddda191ef7c', 'hex')
28-
)
29-
})
30-
31-
it('sha2-512', () => {
32-
const buf = Buffer.from('beep boop')
33-
34-
expect(
35-
multihashing(buf, 'sha2-512')
36-
).to.be.eql(
37-
Buffer.from('134014f301f31be243f34c5668937883771fa381002f1aaa5f31b3f78e500b66ff2f4f8ea5e3c9f5a61bd073e2452c480484b02e030fb239315a2577f7ae156af177', 'hex')
38-
)
39-
})
10+
const tests = {
11+
'sha1': [
12+
['beep boop', '11147c8357577f51d4f0a8d393aa1aaafb28863d9421']
13+
],
14+
'sha2-256': [
15+
['beep boop', '122090ea688e275d580567325032492b597bc77221c62493e76330b85ddda191ef7c']
16+
],
17+
'sha2-512': [
18+
['beep boop', '134014f301f31be243f34c5668937883771fa381002f1aaa5f31b3f78e500b66ff2f4f8ea5e3c9f5a61bd073e2452c480484b02e030fb239315a2577f7ae156af177']
19+
],
20+
'blake2b-512': [
21+
['beep boop', 'c0e402400eac6255ba822373a0948122b8d295008419a8ab27842ee0d70eca39855621463c03ec75ac3610aacfdff89fa989d8d61fc00450148f289eb5b12ad1a954f659']
22+
],
23+
'blake2b-160': [
24+
['beep boop', '94e40214fe303247293e54e0a7ea48f9408ca68b36b08442']
25+
],
26+
'blake2s-256': [
27+
['beep boop', 'e0e402204542eaca484e4311def8af74b546edd7fceb49eeb3cdcfd8a4a72ed0dc81d4c0']
28+
],
29+
'blake2s-40': [
30+
['beep boop', 'c5e402059ada01bb57']
31+
],
32+
}
4033

41-
it('blake2b', () => {
42-
const buf = Buffer.from('beep boop')
43-
44-
expect(
45-
multihashing(buf, 'blake2b-512')
46-
).to.be.eql(
47-
Buffer.from('c0e402400eac6255ba822373a0948122b8d295008419a8ab27842ee0d70eca39855621463c03ec75ac3610aacfdff89fa989d8d61fc00450148f289eb5b12ad1a954f659', 'hex')
48-
)
49-
expect(
50-
multihashing(buf, 'blake2b-160')
51-
).to.be.eql(
52-
Buffer.from('94e40214fe303247293e54e0a7ea48f9408ca68b36b08442', 'hex')
53-
)
54-
})
55-
56-
it('blake2s', () => {
57-
const buf = Buffer.from('beep boop')
58-
59-
expect(
60-
multihashing(buf, 'blake2s-256')
61-
).to.be.eql(
62-
Buffer.from('e0e402204542eaca484e4311def8af74b546edd7fceb49eeb3cdcfd8a4a72ed0dc81d4c0', 'hex')
63-
)
64-
expect(
65-
multihashing(buf, 'blake2s-40')
66-
).to.be.eql(
67-
Buffer.from('c5e402059ada01bb57', 'hex')
68-
)
69-
})
34+
describe('multihashing', () => {
35+
for (const algo in tests) {
36+
it(algo, () => {
37+
for (const test of tests[algo]) {
38+
const input = Buffer.from(test[0])
39+
const output = Buffer.from(test[1], 'hex')
40+
expect(multihashing(input, algo)).to.be.eql(output)
41+
}
42+
})
43+
}
7044

7145
it('cuts the length', () => {
7246
const buf = Buffer.from('beep boop')

0 commit comments

Comments
 (0)