Skip to content

Commit 32c586e

Browse files
authored
Merge branch 'master' into master
2 parents cba2381 + d86299e commit 32c586e

File tree

4 files changed

+63
-50
lines changed

4 files changed

+63
-50
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
.classpath
2+
.project
3+
.settings
14
*.class
25

36
target/**

pom.xml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

5-
<groupId>io.ipfs</groupId>
6-
<artifactId>multihash</artifactId>
7-
<version>1.2.0</version>
5+
<groupId>com.github.multiformats</groupId>
6+
<artifactId>java-multihash</artifactId>
7+
<version>v1.2.1-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>multihash</name>
@@ -32,8 +32,9 @@
3232
<properties>
3333
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3434
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
35-
<junit.version>4.12</junit.version>
36-
<hamcrest.version>1.3</hamcrest.version>
35+
<version.junit>4.12</version.junit>
36+
<version.hamcrest>1.3</version.hamcrest>
37+
<version.multibase>v1.0.0</version.multibase>
3738
</properties>
3839

3940
<repositories>
@@ -44,23 +45,23 @@
4445
</repositories>
4546

4647
<dependencies>
48+
<dependency>
49+
<groupId>com.github.multiformats</groupId>
50+
<artifactId>java-multibase</artifactId>
51+
<version>${version.multibase}</version>
52+
</dependency>
4753
<dependency>
4854
<groupId>junit</groupId>
4955
<artifactId>junit</artifactId>
50-
<version>${junit.version}</version>
56+
<version>${version.junit}</version>
5157
<scope>test</scope>
5258
</dependency>
5359
<dependency>
5460
<groupId>org.hamcrest</groupId>
5561
<artifactId>hamcrest-core</artifactId>
56-
<version>${hamcrest.version}</version>
62+
<version>${version.hamcrest}</version>
5763
<scope>test</scope>
5864
</dependency>
59-
<dependency>
60-
<groupId>com.github.multiformats</groupId>
61-
<artifactId>java-multibase</artifactId>
62-
<version>v1.0.0</version>
63-
</dependency>
6465
</dependencies>
6566

6667
<build>

src/main/java/io/ipfs/multihash/Multihash.java

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package io.ipfs.multihash;
22

3-
import io.ipfs.multibase.*;
3+
import io.ipfs.multibase.Base16;
4+
import io.ipfs.multibase.Base58;
45

5-
import java.io.*;
6-
import java.util.*;
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.DataInput;
8+
import java.io.DataOutput;
9+
import java.io.IOException;
10+
import java.util.Arrays;
11+
import java.util.Map;
12+
import java.util.TreeMap;
713

814
public class Multihash {
915
public enum Type {
@@ -21,9 +27,9 @@ public enum Type {
2127
blake2b(0x40, 64),
2228
blake2s(0x41, 32);
2329

24-
public int index, length;
30+
public final int index, length;
2531

26-
Type(int index, int length) {
32+
Type(final int index, final int length) {
2733
this.index = index;
2834
this.length = length;
2935
}
@@ -39,12 +45,13 @@ public static Type lookup(int t) {
3945
throw new IllegalStateException("Unknown Multihash type: "+t);
4046
return lookup.get(t);
4147
}
48+
4249
}
4350

44-
public final Type type;
51+
private final Type type;
4552
private final byte[] hash;
4653

47-
public Multihash(Type type, byte[] hash) {
54+
public Multihash(final Type type, final byte[] hash) {
4855
if (hash.length > 127)
4956
throw new IllegalStateException("Unsupported hash size: "+hash.length);
5057
if (hash.length != type.length)
@@ -57,7 +64,7 @@ public Multihash(Multihash toClone) {
5764
this(toClone.type, toClone.hash); // N.B. despite being a byte[], hash is immutable
5865
}
5966

60-
public Multihash(byte[] multihash) {
67+
public Multihash(final byte[] multihash) {
6168
this(Type.lookup(multihash[0] & 0xff), Arrays.copyOfRange(multihash, 2, multihash.length));
6269
}
6370

@@ -68,10 +75,13 @@ public byte[] toBytes() {
6875
System.arraycopy(hash, 0, res, 2, hash.length);
6976
return res;
7077
}
78+
79+
public Type getType() {
80+
return type;
81+
}
7182

7283
public byte[] getHash() {
73-
byte[] res = Arrays.copyOf(hash)
74-
return res;
84+
return Arrays.copyOf(hash, hash.length);
7585
}
7686

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

107-
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
108-
109117
public String toHex() {
110-
byte[] bytes = toBytes();
111-
char[] hexChars = new char[bytes.length * 2];
112-
for ( int j = 0; j < bytes.length; j++ ) {
113-
int v = bytes[j] & 0xFF;
114-
hexChars[j * 2] = hexArray[v >>> 4];
115-
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
116-
}
117-
return new String(hexChars);
118+
return Base16.encode(toBytes());
118119
}
119120

120121
public String toBase58() {
@@ -123,11 +124,15 @@ public String toBase58() {
123124

124125
public static Multihash fromHex(String hex) {
125126
if (hex.length() % 2 != 0)
126-
throw new IllegalStateException("Uneven number of hex digits!");
127-
ByteArrayOutputStream bout = new ByteArrayOutputStream();
128-
for (int i=0; i < hex.length()-1; i+= 2)
129-
bout.write(Integer.valueOf(hex.substring(i, i+2), 16));
130-
return new Multihash(bout.toByteArray());
127+
throw new IllegalStateException("Odd number of hex digits!");
128+
129+
try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) {
130+
for (int i = 0; i < hex.length() - 1; i += 2)
131+
bout.write(Integer.valueOf(hex.substring(i, i + 2), 16));
132+
return new Multihash(bout.toByteArray());
133+
} catch (IOException e) {
134+
throw new IllegalStateException("Unable to handle Multihash conversion to Hex properly");
135+
}
131136
}
132137

133138
public static Multihash fromBase58(String base58) {
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package io.ipfs.multihash;
22

3-
import org.junit.*;
4-
import io.ipfs.multibase.*;
5-
import java.util.*;
3+
import io.ipfs.multibase.Base58;
4+
import org.junit.Test;
5+
66
import java.security.MessageDigest;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.*;
711

812
public class MultihashTest {
913

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

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

3336
for(Object[] ex: examples) {
3437
Multihash m = Multihash.fromBase58((String)ex[2]);
3538
try {
3639
MessageDigest md = MessageDigest.getInstance((String) ex[1]);
37-
assert(md != null);
40+
assertNotNull(md );
3841
md.update(((String) ex[3]).getBytes("UTF-8"));
3942
byte[] digest = md.digest();
4043
// Test constructor
4144
Multihash m2 = new Multihash((Multihash.Type)ex[0], digest);
4245
// Test comparison
43-
assert(m2.equals(m));
46+
assertEquals(m, m2);
4447
// Test conversions
45-
assert(m.toBase58().equals(m2.toBase58()));
46-
assert(m.toBase58().equals((String)ex[2]));
48+
assertEquals(m.toBase58(), m2.toBase58());
49+
assertEquals(m.toBase58(), ex[2]);
4750
// Test fromHex and toHex
4851
Multihash m3 = Multihash.fromHex(m.toHex());
49-
assert(m3.equals(m));
52+
assertEquals(m, m3);
5053
}
5154
catch (Exception e){
5255
System.out.println(e.getMessage());
53-
assert(false);
56+
assertTrue(false);
5457
}
5558
}
5659
}
60+
5761
}

0 commit comments

Comments
 (0)