Skip to content

Commit 9a5bdc3

Browse files
committed
feat: add identity multihash
1 parent fdc82c1 commit 9a5bdc3

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ import the ones you need yourself.
141141
| --- | --- | --- |
142142
| `sha2-256`, `sha2-512` | `multiformats/hashes/sha2` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/hashes) |
143143
| `sha3-224`, `sha3-256`, `sha3-384`,`sha3-512`, `shake-128`, `shake-256`, `keccak-224`, `keccak-256`, `keccak-384`, `keccak-512` | `@multiformats/sha3` | [multiformats/js-sha3](https://github.com/multiformats/js-sha3) |
144+
| `identity` | `multiformats/hashes/identity` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/hashes/identity.js) |
144145
| `murmur3-128`, `murmur3-32` | `@multiformats/murmur3` | [multiformats/js-murmur3](https://github.com/multiformats/js-murmur3) |
145146
| `blake2b-*`, `blake2s-*` | `@multiformats/blake2` | [multiformats/js-blake2](https://github.com/multiformats/js-blake2) |
146147

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
"browser": "./src/hashes/sha2-browser.js",
7171
"import": "./src/hashes/sha2.js"
7272
},
73+
"./hashes/identity": {
74+
"import": "./src/hashes/identity.js"
75+
},
7376
"./codecs/codec": {
7477
"import": "./src/codecs/codec.js"
7578
},

src/hashes/identity.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @ts-check
2+
3+
import { from } from './hasher.js'
4+
import { coerce } from '../bytes.js'
5+
6+
export default from({
7+
name: 'identity',
8+
code: 0x0,
9+
encode: (input) => coerce(input)
10+
})

test/test-multihash.js

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import valid from './fixtures/valid-multihash.js'
55
import invalid from './fixtures/invalid-multihash.js'
66
import crypto from 'crypto'
77
import { sha256, sha512, __browser } from 'multiformats/hashes/sha2'
8+
import identity from 'multiformats/hashes/identity'
89
import { decode as decodeDigest, create as createDigest } from 'multiformats/hashes/digest'
910
const test = it
1011
const encode = name => data => coerce(crypto.createHash(name).update(data).digest())
@@ -66,6 +67,16 @@ describe('multihash', () => {
6667
same(hash2.code, sha512.code)
6768
same(hash2.bytes, hash.bytes)
6869
})
70+
test('hash identity', async () => {
71+
const hash = await identity.digest(fromString('test'))
72+
same(hash.code, identity.code)
73+
same(identity.code, 0)
74+
same(hash.digest, fromString('test'))
75+
76+
const hash2 = decodeDigest(hash.bytes)
77+
same(hash2.code, identity.code)
78+
same(hash2.bytes, hash.bytes)
79+
})
6980
})
7081
describe('decode', () => {
7182
for (const { encoding, hex, size } of valid) {

0 commit comments

Comments
 (0)