Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.common.crypto

File Multihash.java

 

Coverage histogram

../../../../img/srcFileCovDistChart4.png
43% of files have more coverage

Code metrics

12
45
18
2
132
109
24
0.53
2.5
9
1.33

Classes

Class Line # Actions
Multihash 14 38 20
0.2380952423.8%
Multihash.Type 15 7 4
0.833333383.3%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    package io.sunshower.common.crypto;
2   
3    import io.sunshower.encodings.Base58;
4    import java.io.*;
5    import java.util.*;
6    import javax.persistence.*;
7    import javax.xml.bind.annotation.XmlAttribute;
8    import javax.xml.bind.annotation.XmlElement;
9    import javax.xml.bind.annotation.XmlRootElement;
10    import javax.xml.bind.annotation.XmlSchemaType;
11   
12    @Embeddable
13    @XmlRootElement
 
14    public class Multihash implements Serializable {
 
15    public enum Type {
16    SHA1(0x11, 20),
17    SHA_2_256(0x12, 32),
18    SHA_2_512(0x13, 64),
19    SHA_3(0x14, 64),
20    BLAKE_2_B(0x40, 64),
21    BLAKE_2_S(0x41, 32);
22    public int index, length;
23   
 
24  6 toggle Type(int index, int length) {
25  6 this.index = index;
26  6 this.length = length;
27    }
28   
29    private static Map<Integer, Type> lookup = new TreeMap<>();
30   
 
31  1 toggle static {
32  6 for (Type t : Type.values()) lookup.put(t.index, t);
33    }
34   
 
35  1 toggle public static Type lookup(int t) {
36  1 if (!lookup.containsKey(t)) throw new IllegalStateException("Unknown Multihash type: " + t);
37  1 return lookup.get(t);
38    }
39    }
40   
41    @Column(insertable = false, updatable = false)
42    @XmlAttribute
43    @Enumerated(EnumType.ORDINAL)
44    private Type type;
45   
46    @Column(insertable = false, updatable = false)
47    @XmlElement(name = "address")
48    @XmlSchemaType(name = "hex64Binary")
49    private byte[] hash;
50   
 
51  0 toggle public Multihash() {}
52   
 
53  3 toggle public Multihash(Type type, byte[] hash) {
54  3 if (hash.length > 127) throw new IllegalStateException("Unsupported hash size: " + hash.length);
55  3 if (hash.length != type.length)
56  0 throw new IllegalStateException(
57    "Incorrect hash length: " + hash.length + " != " + type.length);
58  3 this.type = type;
59  3 this.hash = hash;
60    }
61   
 
62  1 toggle public Multihash(byte[] multihash) {
63  1 this(Type.lookup(multihash[0] & 0xff), Arrays.copyOfRange(multihash, 2, multihash.length));
64    }
65   
 
66  0 toggle public Type getType() {
67  0 return type;
68    }
69   
 
70  0 toggle public byte[] toBytes() {
71  0 byte[] res = new byte[hash.length + 2];
72  0 res[0] = (byte) type.index;
73  0 res[1] = (byte) hash.length;
74  0 System.arraycopy(hash, 0, res, 2, hash.length);
75  0 return res;
76    }
77   
 
78  0 toggle public void serialize(DataOutput dout) throws IOException {
79  0 dout.write(toBytes());
80    }
81   
 
82  0 toggle public static Multihash deserialize(DataInput din) throws IOException {
83  0 int type = din.readUnsignedByte();
84  0 int len = din.readUnsignedByte();
85  0 Type t = Type.lookup(type);
86  0 byte[] hash = new byte[len];
87  0 din.readFully(hash);
88  0 return new Multihash(t, hash);
89    }
90   
 
91  0 toggle @Override
92    public String toString() {
93  0 return toBase58();
94    }
95   
 
96  1 toggle @Override
97    public boolean equals(Object o) {
98  1 if (!(o instanceof Multihash)) return false;
99  1 return type == ((Multihash) o).type && Arrays.equals(hash, ((Multihash) o).hash);
100    }
101   
 
102  0 toggle @Override
103    public int hashCode() {
104  0 return Arrays.hashCode(hash) ^ type.hashCode();
105    }
106   
 
107  0 toggle public String toHex() {
108  0 StringBuilder res = new StringBuilder();
109  0 for (byte b : toBytes()) res.append(String.format("%x", b & 0xff));
110  0 return res.toString();
111    }
112   
 
113  0 toggle public static Multihash fromString(String s) {
114  0 return fromBase58(s);
115    }
116   
 
117  0 toggle public String toBase58() {
118  0 return Base58.getInstance(Base58.Alphabets.Default).encode(toBytes());
119    }
120   
 
121  0 toggle public static Multihash fromHex(String hex) {
122  0 if (hex.length() % 2 != 0) throw new IllegalStateException("Uneven number of hex digits!");
123  0 ByteArrayOutputStream bout = new ByteArrayOutputStream();
124  0 for (int i = 0; i < hex.length() - 1; i += 2)
125  0 bout.write(Integer.valueOf(hex.substring(i, i + 2), 16));
126  0 return new Multihash(bout.toByteArray());
127    }
128   
 
129  1 toggle public static Multihash fromBase58(String base58) {
130  1 return new Multihash(Base58.getInstance(Base58.Alphabets.Default).decode(base58));
131    }
132    }