File tree Expand file tree Collapse file tree
main/java/io/ipfs/multihash
test/java/io/ipfs/multihash Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -218,6 +218,18 @@ public static Multihash deserialize(byte[] raw) throws IOException {
218218 return deserialize (new ByteArrayInputStream (raw ));
219219 }
220220
221+ /**
222+ *
223+ * @param encoded A multibase encoded serialization of a Multihash
224+ * @return
225+ * @throws IOException
226+ */
227+ public static Multihash decode (String encoded ) throws IOException {
228+ if (encoded .length () == 46 && encoded .startsWith ("Qm" ))
229+ return deserialize (Base58 .decode (encoded ));
230+ return deserialize (Multibase .decode (encoded ));
231+ }
232+
221233 @ Override
222234 public String toString () {
223235 return toBase58 ();
Original file line number Diff line number Diff line change 11package io .ipfs .multihash ;
22
3- import io .ipfs .multibase .Base58 ;
3+ import io .ipfs .multibase .* ;
44import org .junit .Test ;
55
6+ import java .io .*;
67import java .security .MessageDigest ;
78import java .util .Arrays ;
89import java .util .List ;
@@ -22,6 +23,22 @@ public void base58Test() {
2223 }
2324 }
2425
26+ @ Test
27+ public void decodeTest () throws IOException {
28+ List <String > base58 = Arrays .asList (
29+ "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB" ,
30+ "QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy"
31+ );
32+ List <String > base36 = Arrays .asList (
33+ "kmue2y4illvr0m3lt8x6z8iwghtxlzdmkjh957p5rr5cdr9243ugc" ,
34+ "kmuia3qyasz2z5cnz848bag5n5wfc7gzi35cz2npamtmkyifd5anu" );
35+ for (int i =0 ; i < base58 .size (); i ++) {
36+ Multihash b58 = Multihash .decode (base58 .get (i ));
37+ Multihash b36 = Multihash .decode (base36 .get (i ));
38+ assertEquals (b58 , b36 );
39+ }
40+ }
41+
2542 @ Test
2643 public void multihashTest () {
2744 Object [][] examples = new Object [][]{
You can’t perform that action at this time.
0 commit comments