Clover icon

sunshower-base

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

File Multihash.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
25% of files have more coverage

Code metrics

14
52
13
3
124
103
26
0.5
4
4.33
2

Classes

Class Line # Actions
Multihash 5 35 17
0.6271186562.7%
Multihash.Type 7 16 8
0.944444494.4%
Multihash.Encoding 114 1 1
0.00%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    package io.sunshower.lang.common.hash;
2   
3    import java.util.Arrays;
4   
 
5    public final class Multihash {
6   
 
7    public enum Type {
8    SHA1(0x11, 20),
9    SHA256(0x12, 32),
10    SHA512(0x13, 64),
11    SHA3(0x14, 64),
12    BLAKE2B(0x40, 64),
13    BLAKE2S(0x41, 32);
14   
15    final int length;
16    final int value;
17   
 
18  6 toggle Type(final int value, final int length) {
19  6 this.value = value;
20  6 this.length = length;
21    }
22   
 
23  14 toggle static Type get(int value) {
24  14 switch (value) {
25  4 case 0x11:
26  4 return SHA1;
27  2 case 0x12:
28  2 return SHA256;
29  2 case 0x13:
30  2 return SHA512;
31  2 case 0x14:
32  2 return SHA3;
33  2 case 0x40:
34  2 return BLAKE2B;
35  2 case 0x41:
36  2 return BLAKE2S;
37    }
38  0 throw new InvalidHashException("No hash identified by '" + value + "' was found!");
39    }
40    }
41   
42    final Type type;
43    final byte[] data;
44   
 
45  14 toggle private Multihash(Type type, byte[] data) {
46  14 this.type = type;
47  14 this.data = data;
48    }
49   
 
50  0 toggle public String encode(Encoding encoding) {
51  0 return new String(encoding.encode(data));
52    }
53   
 
54  1 toggle public Type getType() {
55  1 return this.type;
56    }
57   
 
58  14 toggle public static Multihash decode(byte[] data) {
59  14 check(data);
60  14 final Type type = Type.get(data[0]);
61  14 final int length = data[1] & 0xFF;
62  14 final byte[] digest = new byte[length];
63  14 System.arraycopy(data, 2, digest, 0, length);
64  14 return new Multihash(type, digest);
65    }
66   
 
67  7 toggle public static byte[] encode(Multihash hash) {
68  7 return hash.toByteArray();
69    }
70   
 
71  7 toggle public byte[] toByteArray() {
72  7 final byte[] result = new byte[data.length + 2];
73  7 result[0] = (byte) type.value;
74  7 result[1] = (byte) data.length;
75  7 System.arraycopy(data, 0, result, 2, data.length);
76  7 return result;
77    }
78   
 
79  0 toggle @Override
80    public String toString() {
81  0 return "Multihash{" + "type=" + type + ", data=" + Arrays.toString(data) + '}';
82    }
83   
 
84  6 toggle public boolean equals(Object o) {
85  6 if (o == null) return false;
86  6 if (o == this) return true;
87  6 if (Multihash.class.equals(o.getClass())) {
88  6 final Multihash that = (Multihash) o;
89  6 return that.type == this.type && Arrays.equals(this.data, that.data);
90    }
91  0 return false;
92    }
93   
 
94  0 toggle @Override
95    public int hashCode() {
96  0 int result = type != null ? type.hashCode() : 0;
97  0 result = 31 * result + Arrays.hashCode(data);
98  0 return result;
99    }
100   
 
101  14 toggle private static void check(byte[] data) {
102  14 if (data == null) {
103  0 throw new InvalidHashException("Hash cannot be null");
104    }
105  14 final int length = data.length;
106  14 if (length < 3) {
107  0 throw new InvalidHashException("Hash must be at least 3 bytes long");
108    }
109  14 if (length > 129) {
110  0 throw new InvalidHashException("Hash must not be longer than 129 bytes");
111    }
112    }
113   
 
114    interface Encoding {
115   
116    byte[] decode(byte[] input);
117   
118    byte[] encode(byte[] input);
119   
 
120  0 toggle default byte[] encode(String input) {
121  0 return encode(input.getBytes());
122    }
123    }
124    }