Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
veqtor authored Aug 9, 2018
2 parents cba2381 + d86299e commit 32c586e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.classpath
.project
.settings
*.class

target/**
Expand Down
25 changes: 13 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.ipfs</groupId>
<artifactId>multihash</artifactId>
<version>1.2.0</version>
<groupId>com.github.multiformats</groupId>
<artifactId>java-multihash</artifactId>
<version>v1.2.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>multihash</name>
Expand Down Expand Up @@ -32,8 +32,9 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<junit.version>4.12</junit.version>
<hamcrest.version>1.3</hamcrest.version>
<version.junit>4.12</version.junit>
<version.hamcrest>1.3</version.hamcrest>
<version.multibase>v1.0.0</version.multibase>
</properties>

<repositories>
Expand All @@ -44,23 +45,23 @@
</repositories>

<dependencies>
<dependency>
<groupId>com.github.multiformats</groupId>
<artifactId>java-multibase</artifactId>
<version>${version.multibase}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${hamcrest.version}</version>
<version>${version.hamcrest}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.multiformats</groupId>
<artifactId>java-multibase</artifactId>
<version>v1.0.0</version>
</dependency>
</dependencies>

<build>
Expand Down
55 changes: 30 additions & 25 deletions src/main/java/io/ipfs/multihash/Multihash.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package io.ipfs.multihash;

import io.ipfs.multibase.*;
import io.ipfs.multibase.Base16;
import io.ipfs.multibase.Base58;

import java.io.*;
import java.util.*;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

public class Multihash {
public enum Type {
Expand All @@ -21,9 +27,9 @@ public enum Type {
blake2b(0x40, 64),
blake2s(0x41, 32);

public int index, length;
public final int index, length;

Type(int index, int length) {
Type(final int index, final int length) {
this.index = index;
this.length = length;
}
Expand All @@ -39,12 +45,13 @@ public static Type lookup(int t) {
throw new IllegalStateException("Unknown Multihash type: "+t);
return lookup.get(t);
}

}

public final Type type;
private final Type type;
private final byte[] hash;

public Multihash(Type type, byte[] hash) {
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)
Expand All @@ -57,7 +64,7 @@ public Multihash(Multihash toClone) {
this(toClone.type, toClone.hash); // N.B. despite being a byte[], hash is immutable
}

public Multihash(byte[] multihash) {
public Multihash(final byte[] multihash) {
this(Type.lookup(multihash[0] & 0xff), Arrays.copyOfRange(multihash, 2, multihash.length));
}

Expand All @@ -68,10 +75,13 @@ public byte[] toBytes() {
System.arraycopy(hash, 0, res, 2, hash.length);
return res;
}

public Type getType() {
return type;
}

public byte[] getHash() {
byte[] res = Arrays.copyOf(hash)
return res;
return Arrays.copyOf(hash, hash.length);
}

public void serialize(DataOutput dout) throws IOException {
Expand Down Expand Up @@ -104,17 +114,8 @@ public int hashCode() {
return Arrays.hashCode(hash) ^ type.hashCode();
}

final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

public String toHex() {
byte[] bytes = toBytes();
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
return Base16.encode(toBytes());
}

public String toBase58() {
Expand All @@ -123,11 +124,15 @@ public String toBase58() {

public static Multihash fromHex(String hex) {
if (hex.length() % 2 != 0)
throw new IllegalStateException("Uneven number of hex digits!");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
for (int i=0; i < hex.length()-1; i+= 2)
bout.write(Integer.valueOf(hex.substring(i, i+2), 16));
return new Multihash(bout.toByteArray());
throw new IllegalStateException("Odd number of hex digits!");

try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) {
for (int i = 0; i < hex.length() - 1; i += 2)
bout.write(Integer.valueOf(hex.substring(i, i + 2), 16));
return new Multihash(bout.toByteArray());
} catch (IOException e) {
throw new IllegalStateException("Unable to handle Multihash conversion to Hex properly");
}
}

public static Multihash fromBase58(String base58) {
Expand Down
30 changes: 17 additions & 13 deletions src/test/java/io/ipfs/multihash/MultihashTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.ipfs.multihash;

import org.junit.*;
import io.ipfs.multibase.*;
import java.util.*;
import io.ipfs.multibase.Base58;
import org.junit.Test;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

public class MultihashTest {

Expand All @@ -14,8 +18,7 @@ public void base58Test() {
for (String example: examples) {
byte[] output = Base58.decode(example);
String encoded = Base58.encode(output);
if (!encoded.equals(example))
throw new IllegalStateException("Incorrect base58! " + example + " => " + encoded);
assertEquals(example, encoded);
}
}

Expand All @@ -26,32 +29,33 @@ public void multihashTest() {
{Multihash.Type.sha1, "SHA-1", "5drNu81uhrFLRiS4bxWgAkpydaLUPW", "hello world"},
{Multihash.Type.sha2_256, "SHA-256", "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4", "hello world"},
{Multihash.Type.sha2_512, "SHA-512", "8Vtkv2tdQ43bNGdWN9vNx9GVS9wrbXHk4ZW8kmucPmaYJwwedXir52kti9wJhcik4HehyqgLrQ1hBuirviLhxgRBNv", "hello world"}
// SHA3 not yet implemented in standard Java library. https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest
// {Multihash.Type.sha3_512, "SHA3-512", "8tWhXW5oUwtPd9d3FnjuLP1NozN3vc45rmsoWEEfrZL1L6gi9dqi1YkZu5iHb2HJ8WbZaaKAyNWWRAa8yaxMkGKJmX", "hello world"}
// SHA3 not yet implemented in standard Java library. https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest
// {Multihash.Type.sha3_512, "SHA3-512", "8tWhXW5oUwtPd9d3FnjuLP1NozN3vc45rmsoWEEfrZL1L6gi9dqi1YkZu5iHb2HJ8WbZaaKAyNWWRAa8yaxMkGKJmX", "hello world"}
};

for(Object[] ex: examples) {
Multihash m = Multihash.fromBase58((String)ex[2]);
try {
MessageDigest md = MessageDigest.getInstance((String) ex[1]);
assert(md != null);
assertNotNull(md );
md.update(((String) ex[3]).getBytes("UTF-8"));
byte[] digest = md.digest();
// Test constructor
Multihash m2 = new Multihash((Multihash.Type)ex[0], digest);
// Test comparison
assert(m2.equals(m));
assertEquals(m, m2);
// Test conversions
assert(m.toBase58().equals(m2.toBase58()));
assert(m.toBase58().equals((String)ex[2]));
assertEquals(m.toBase58(), m2.toBase58());
assertEquals(m.toBase58(), ex[2]);
// Test fromHex and toHex
Multihash m3 = Multihash.fromHex(m.toHex());
assert(m3.equals(m));
assertEquals(m, m3);
}
catch (Exception e){
System.out.println(e.getMessage());
assert(false);
assertTrue(false);
}
}
}

}

0 comments on commit 32c586e

Please sign in to comment.