-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMultihash.java
247 lines (220 loc) · 7.3 KB
/
Multihash.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package io.ipfs.multihash;
import io.ipfs.multibase.Base16;
import io.ipfs.multibase.Base58;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Multihash {
public enum Type {
id(0, -1),
md5(0xd5, 16),
sha1(0x11, 20),
sha2_256(0x12, 32),
sha2_512(0x13, 64),
dbl_sha2_256(0x56, 32),
sha3_224(0x17, 24),
sha3_256(0x16, 32),
sha3_512(0x14, 64),
shake_128(0x18, 32),
shake_256(0x19, 64),
keccak_224(0x1a, 24),
keccak_256(0x1b, 32),
keccak_384(0x1c, 48),
keccak_512(0x1d, 64),
murmur3(0x22, 4),
// blake2b (64 codes)
blake2b_8(0xb201, 1),
blake2b_16(0xb202, 2),
blake2b_24(0xb203, 3),
blake2b_32(0xb204, 4),
blake2b_40(0xb205, 5),
blake2b_48(0xb206, 6),
blake2b_56(0xb207, 7),
blake2b_64(0xb208, 8),
blake2b_72(0xb209, 9),
blake2b_80(0xb20a, 10),
blake2b_88(0xb20b, 11),
blake2b_96(0xb20c, 12),
blake2b_104(0xb20d, 13),
blake2b_112(0xb20e, 14),
blake2b_120(0xb20f, 15),
blake2b_128(0xb210, 16),
blake2b_136(0xb211, 17),
blake2b_144(0xb212, 18),
blake2b_152(0xb213, 19),
blake2b_160(0xb214, 20),
blake2b_168(0xb215, 21),
blake2b_176(0xb216, 22),
blake2b_184(0xb217, 23),
blake2b_192(0xb218, 24),
blake2b_200(0xb219, 25),
blake2b_208(0xb21a, 26),
blake2b_216(0xb21b, 27),
blake2b_224(0xb21c, 28),
blake2b_232(0xb21d, 29),
blake2b_240(0xb21e, 30),
blake2b_248(0xb21f, 31),
blake2b_256(0xb220, 32),
blake2b_264(0xb221, 33),
blake2b_272(0xb222, 34),
blake2b_280(0xb223, 35),
blake2b_288(0xb224, 36),
blake2b_296(0xb225, 37),
blake2b_304(0xb226, 38),
blake2b_312(0xb227, 39),
blake2b_320(0xb228, 40),
blake2b_328(0xb229, 41),
blake2b_336(0xb22a, 42),
blake2b_344(0xb22b, 43),
blake2b_352(0xb22c, 44),
blake2b_360(0xb22d, 45),
blake2b_368(0xb22e, 46),
blake2b_376(0xb22f, 47),
blake2b_384(0xb230, 48),
blake2b_392(0xb231, 49),
blake2b_400(0xb232, 50),
blake2b_408(0xb233, 51),
blake2b_416(0xb234, 52),
blake2b_424(0xb235, 53),
blake2b_432(0xb236, 54),
blake2b_440(0xb237, 55),
blake2b_448(0xb238, 56),
blake2b_456(0xb239, 57),
blake2b_464(0xb23a, 58),
blake2b_472(0xb23b, 59),
blake2b_480(0xb23c, 60),
blake2b_488(0xb23d, 61),
blake2b_496(0xb23e, 62),
blake2b_504(0xb23f, 63),
blake2b_512(0xb240, 64),
// blake2s (32 codes)
blake2s_8(0xb241, 1),
blake2s_16(0xb242, 2),
blake2s_24(0xb243, 3),
blake2s_32(0xb244, 4),
blake2s_40(0xb245, 5),
blake2s_48(0xb246, 6),
blake2s_56(0xb247, 7),
blake2s_64(0xb248, 8),
blake2s_72(0xb249, 9),
blake2s_80(0xb24a, 10),
blake2s_88(0xb24b, 11),
blake2s_96(0xb24c, 12),
blake2s_104(0xb24d, 13),
blake2s_112(0xb24e, 14),
blake2s_120(0xb24f, 15),
blake2s_128(0xb250, 16),
blake2s_136(0xb251, 17),
blake2s_144(0xb252, 18),
blake2s_152(0xb253, 19),
blake2s_160(0xb254, 20),
blake2s_168(0xb255, 21),
blake2s_176(0xb256, 22),
blake2s_184(0xb257, 23),
blake2s_192(0xb258, 24),
blake2s_200(0xb259, 25),
blake2s_208(0xb25a, 26),
blake2s_216(0xb25b, 27),
blake2s_224(0xb25c, 28),
blake2s_232(0xb25d, 29),
blake2s_240(0xb25e, 30),
blake2s_248(0xb25f, 31),
blake2s_256(0xb260, 32);
public final int index, length;
Type(final int index, final int length) {
this.index = index;
this.length = length;
}
private static Map<Integer, Type> lookup = new HashMap<>();
static {
for (Type t: Type.values())
lookup.put(t.index, t);
}
public static Type lookup(int t) {
Type type = lookup.get(t);
if (type == null)
throw new IllegalStateException("Unknown Multihash type: "+t);
return type;
}
}
private final Type type;
private final 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 && 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;
}
public Multihash(Multihash toClone) {
this(toClone.type, toClone.hash); // N.B. despite being a byte[], hash is immutable
}
public Multihash(final byte[] multihash) {
this(Type.lookup(multihash[0] & 0xff), Arrays.copyOfRange(multihash, 2, multihash.length));
}
public byte[] toBytes() {
byte[] res = new byte[hash.length+2];
res[0] = (byte)type.index;
res[1] = (byte)hash.length;
System.arraycopy(hash, 0, res, 2, hash.length);
return res;
}
public Type getType() {
return type;
}
public byte[] getHash() {
return Arrays.copyOf(hash, hash.length);
}
public void serialize(DataOutput dout) throws IOException {
dout.write(toBytes());
}
public static Multihash deserialize(DataInput din) throws IOException {
int type = din.readUnsignedByte();
int len = din.readUnsignedByte();
Type t = Type.lookup(type);
byte[] hash = new byte[len];
din.readFully(hash);
return new Multihash(t, hash);
}
@Override
public String toString() {
return toBase58();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Multihash))
return false;
return type == ((Multihash) o).type && Arrays.equals(hash, ((Multihash) o).hash);
}
@Override
public int hashCode() {
return Arrays.hashCode(hash) ^ type.hashCode();
}
public String toHex() {
return Base16.encode(toBytes());
}
public String toBase58() {
return Base58.encode(toBytes());
}
public static Multihash fromHex(String hex) {
if (hex.length() % 2 != 0)
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) {
return new Multihash(Base58.decode(base58));
}
}