Clover icon

sunshower-base

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

File Hashes.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
9
7
4
61
47
8
0.89
1.29
1.75
1.14

Classes

Class Line # Actions
Hashes 7 1 1
1.0100%
Hashes.HashCode 9 4 3
0.833333383.3%
Hashes.Algorithm 26 2 2
1.0100%
Hashes.DefaultHashCode 49 2 2
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    package io.sunshower.common;
2   
3    import java.nio.ByteBuffer;
4    import java.security.MessageDigest;
5    import java.security.NoSuchAlgorithmException;
6   
 
7    public class Hashes {
8   
 
9    public interface HashCode {
10   
11    Algorithm getAlgorithm();
12   
 
13  1 toggle default byte[] digest(ByteBuffer buf) {
14  1 return digest(buf.array());
15    }
16   
 
17  1 toggle default byte[] digest(byte[] data) {
18  1 try {
19  1 return MessageDigest.getInstance(getAlgorithm().getName()).digest(data);
20    } catch (NoSuchAlgorithmException e) {
21  0 throw new IllegalStateException(e);
22    }
23    }
24    }
25   
 
26    public enum Algorithm {
27    MD2("MD2"),
28    MD5("MD5"),
29    SHA1("SHA-1"),
30    SHA256("SHA-256"),
31    SHA384("SHA-384"),
32    SHA512("SHA-512");
33   
34    private final String name;
35   
 
36  6 toggle Algorithm(String name) {
37  6 this.name = name;
38    }
39   
 
40  1 toggle public String getName() {
41  1 return name;
42    }
43    }
44   
 
45  1 toggle public static HashCode hashCode(Algorithm algorithm) {
46  1 return new DefaultHashCode(algorithm);
47    }
48   
 
49    private static class DefaultHashCode implements HashCode {
50    final Algorithm algorithm;
51   
 
52  1 toggle private DefaultHashCode(final Algorithm algorithm) {
53  1 this.algorithm = algorithm;
54    }
55   
 
56  1 toggle @Override
57    public Algorithm getAlgorithm() {
58  1 return algorithm;
59    }
60    }
61    }