Skip to content

Support for SHAKE128 and SHAKE256 hashes of arbitrary length #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions src/main/java/io/ipfs/multihash/Multihash.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public enum Type {
sha3_224(0x17, 24),
sha3_256(0x16, 32),
sha3_512(0x14, 64),
shake_128(0x18, 32),
shake_256(0x19, 64),
shake_128(0x18, -1),
shake_256(0x19, -1),
keccak_224(0x1a, 24),
keccak_256(0x1b, 32),
keccak_384(0x1c, 48),
Expand Down Expand Up @@ -158,7 +158,7 @@ public Multihash(Type type, byte[] hash) {
throw new IllegalStateException("Unsupported hash size: "+hash.length);
if (hash.length > MAX_IDENTITY_HASH_LENGTH)
throw new IllegalStateException("Unsupported hash size: "+hash.length);
if (hash.length != type.length && type != Type.id)
if (hash.length != type.length && type.length != -1)
throw new IllegalStateException("Incorrect hash length: " + hash.length + " != "+type.length);
this.type = type;
this.hash = hash;
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/io/ipfs/multihash/MultihashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ void decodeTest() throws IOException {
}
}

@Test
void variableLengthMultihashTest() throws Exception {
Object[][] examples = new Object[][]{
{Multihash.Type.shake_128, "18108eb4b6a932f280335ee1a279f8c208a3"},
{Multihash.Type.shake_128, "18208eb4b6a932f280335ee1a279f8c208a349e7bc65daf831d3021c213825292463"},
{Multihash.Type.shake_128, "18308eb4b6a932f280335ee1a279f8c208a349e7bc65daf831d3021c213825292463c59e22d0fe2c767cd7cacc4df42dd5f6"},
};
for(Object[] ex: examples) {
Multihash m = Multihash.fromHex((String)ex[1]);
assertEquals(ex[0], m.getType());
}
}

@Test
void multihashTest() {
Object[][] examples = new Object[][]{
Expand Down