Clover icon

sunshower-base

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

File Hashes.java

 

Coverage histogram

../../../../img/srcFileCovDistChart3.png
45% of files have more coverage

Code metrics

4
29
6
4
89
74
12
0.41
4.83
1.5
2

Classes

Class Line # Actions
Hashes 10 5 2
1.0100%
Hashes.HashFunction 26 0 0
-1.0 -
Hashes.SHA256 32 12 5
0.2525%
Hashes.SHA512 61 12 5
0.00%
 

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 io.sunshower.encodings.Encoding;
5    import java.security.MessageDigest;
6    import java.security.NoSuchAlgorithmException;
7    import java.util.EnumMap;
8    import java.util.Map;
9   
 
10    public class Hashes {
11    static final Encoding encoding;
12   
13    static final Map<Multihash.Type, HashFunction> hashes;
14   
 
15  1 toggle static {
16  1 encoding = Base58.getInstance(Base58.Alphabets.Default);
17  1 hashes = new EnumMap<>(Multihash.Type.class);
18  1 hashes.put(Multihash.Type.SHA_2_256, new SHA256());
19  1 hashes.put(Multihash.Type.SHA_2_512, new SHA512());
20    }
21   
 
22  1 toggle public static HashFunction create(Multihash.Type type) {
23  1 return hashes.get(type);
24    }
25   
 
26    public interface HashFunction {
27    String hash(Object... objects);
28   
29    Multihash compute(byte[] data);
30    }
31   
 
32    public static class SHA256 implements HashFunction {
33   
 
34  0 toggle @Override
35    public String hash(Object... objects) {
36  0 try {
37  0 final MessageDigest digest = MessageDigest.getInstance("SHA-256");
38  0 final StringBuilder b = new StringBuilder();
39  0 for (Object o : objects) {
40  0 final byte[] values = o == null ? "".getBytes() : o.toString().getBytes();
41   
42  0 b.append(encoding.encode(values));
43    }
44  0 return encoding.encode(digest.digest(b.toString().getBytes()));
45    } catch (NoSuchAlgorithmException e) {
46  0 throw new RuntimeException(e);
47    }
48    }
49   
 
50  2 toggle @Override
51    public Multihash compute(byte[] data) {
52  2 try {
53  2 final MessageDigest digest = MessageDigest.getInstance("SHA-256");
54  2 return new Multihash(Multihash.Type.SHA_2_256, digest.digest(data));
55    } catch (NoSuchAlgorithmException e) {
56  0 throw new RuntimeException(e);
57    }
58    }
59    }
60   
 
61    public static class SHA512 implements HashFunction {
62   
 
63  0 toggle @Override
64    public String hash(Object... objects) {
65  0 try {
66  0 final MessageDigest digest = MessageDigest.getInstance("SHA-512");
67  0 final StringBuilder b = new StringBuilder();
68  0 for (Object o : objects) {
69  0 final byte[] values = o == null ? "".getBytes() : o.toString().getBytes();
70   
71  0 b.append(encoding.encode(values));
72    }
73  0 return encoding.encode(digest.digest(b.toString().getBytes()));
74    } catch (NoSuchAlgorithmException e) {
75  0 throw new RuntimeException(e);
76    }
77    }
78   
 
79  0 toggle @Override
80    public Multihash compute(byte[] data) {
81  0 try {
82  0 final MessageDigest digest = MessageDigest.getInstance("SHA-512");
83  0 return new Multihash(Multihash.Type.SHA_2_512, digest.digest(data));
84    } catch (NoSuchAlgorithmException e) {
85  0 throw new RuntimeException(e);
86    }
87    }
88    }
89    }