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

Commit 3d4958e

Browse files
Gozalahugomrdias
authored andcommitted
fix: add identity hashing (#53)
* fix: add identity hashing * fix: ensure that `Uint8Array` instance can be passed
1 parent 536f588 commit 3d4958e

File tree

5 files changed

+17
-0
lines changed

5 files changed

+17
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"Richard Schneider <[email protected]>",
7272
"Victor Bjelkholm <[email protected]>",
7373
"Volker Mische <[email protected]>",
74+
"Irakli Gozalishvili <[email protected]>",
7475
"npm-to-cdn-bot (by Forbes Lindesay) <[email protected]>"
7576
]
7677
}

src/crypto.js

+3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ const hash = (algorithm) => async (data) => {
4242
}
4343
}
4444

45+
const identity = data => Buffer.from(data)
46+
4547
module.exports = {
48+
identity,
4649
sha1: sha('sha1'),
4750
sha2256: sha('sha2-256'),
4851
sha2512: sha('sha2-512'),

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ Multihashing.createHash = function (alg) {
6666
* @type {Object}
6767
*/
6868
Multihashing.functions = {
69+
// identity
70+
0x00: crypto.identity,
6971
// sha1
7072
0x11: crypto.sha1,
7173
// sha2-256

test/fixtures/encodes.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict'
22

33
module.exports = [[
4+
'beep boop',
5+
'identity',
6+
'00096265657020626f6f70'
7+
], [
48
'beep boop',
59
'sha1',
610
'11147c8357577f51d4f0a8d393aa1aaafb28863d9421'

test/index.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ describe('multihashing', () => {
2121
const digest = await multihashing(Buffer.from(raw), func)
2222
expect(digest.toString('hex')).to.eql(encoded)
2323
})
24+
25+
it(`encodes Uint8Array in ${func}`, async function () {
26+
const buffer = Buffer.from(raw)
27+
const bytes = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
28+
const digest = await multihashing(bytes, func)
29+
expect(digest.toString('hex')).to.eql(encoded)
30+
})
2431
}
2532

2633
it('cuts the length', async () => {

0 commit comments

Comments
 (0)