Skip to content

Commit

Permalink
Update multibase, and support identity hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
ianopolous committed Aug 14, 2018
1 parent 628392e commit db94327
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Binary file modified lib/multibase.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<version.junit>4.12</version.junit>
<version.hamcrest>1.3</version.hamcrest>
<version.multibase>v1.0.0</version.multibase>
<version.multibase>v1.0.1</version.multibase>
</properties>

<repositories>
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/ipfs/multihash/Multihash.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

public class Multihash {
public enum Type {
id(0, -1),
md5(0xd5, 16),
sha1(0x11, 20),
sha2_256(0x12, 32),
Expand Down Expand Up @@ -54,8 +55,10 @@ public static Type lookup(int t) {
public Multihash(final Type type, final byte[] hash) {
if (hash.length > 127)
throw new IllegalStateException("Unsupported hash size: "+hash.length);
if (hash.length != type.length)
if (hash.length != type.length && type != Type.id)
throw new IllegalStateException("Incorrect hash length: " + hash.length + " != "+type.length);
if (type == Type.id && hash.length > 64)
throw new IllegalStateException("Unsupported size for identity hash! "+ hash.length);
this.type = type;
this.hash = hash;
}
Expand Down
14 changes: 10 additions & 4 deletions src/test/java/io/ipfs/multihash/MultihashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public void base58Test() {
@Test
public void multihashTest() {
Object[][] examples = new Object[][]{
{Multihash.Type.id, "ID", "13hC12xCn", "hello"},
{Multihash.Type.id, "ID", "11", ""},
{Multihash.Type.md5, "MD5", "9qZY4e2uauH3bG83FdaPSaPzA", "hello world"},
{Multihash.Type.sha1, "SHA-1", "5drNu81uhrFLRiS4bxWgAkpydaLUPW", "hello world"},
{Multihash.Type.sha2_256, "SHA-256", "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4", "hello world"},
Expand All @@ -36,10 +38,14 @@ public void multihashTest() {
for(Object[] ex: examples) {
Multihash m = Multihash.fromBase58((String)ex[2]);
try {
MessageDigest md = MessageDigest.getInstance((String) ex[1]);
assertNotNull(md );
md.update(((String) ex[3]).getBytes("UTF-8"));
byte[] digest = md.digest();
byte[] digest;
if (ex[0] != Multihash.Type.id) {
MessageDigest md = MessageDigest.getInstance((String) ex[1]);
assertNotNull(md);
md.update(((String) ex[3]).getBytes("UTF-8"));
digest = md.digest();
} else
digest = ((String) ex[3]).getBytes("UTF-8");
// Test constructor
Multihash m2 = new Multihash((Multihash.Type)ex[0], digest);
// Test comparison
Expand Down

0 comments on commit db94327

Please sign in to comment.