blob: 90e7b1a0b0e8bba3421ba7f1e8aa1624bc2c6b82 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.libg.cryptography;
2
3import java.util.*;
4
5import aQute.lib.hex.*;
6
7public abstract class Digest {
8 final byte[] digest;
9
10 protected Digest(byte[] checksum, int width) {
11 this.digest = checksum;
12 if (digest.length != width)
13 throw new IllegalArgumentException("Invalid width for digest: " + digest.length
14 + " expected " + width);
15 }
16
17
18 public byte[] digest() {
19 return digest;
20 }
21
22 @Override public String toString() {
23 return String.format("%s(d=%s)", getAlgorithm(), Hex.toHexString(digest));
24 }
25
26 public abstract String getAlgorithm();
27
28
29 public boolean equals(Object other) {
30 if ( !(other instanceof Digest))
31 return false;
32
33 Digest d = (Digest) other;
34 return Arrays.equals(d.digest, digest);
35 }
36
37 public int hashCode() {
38 return Arrays.hashCode(digest);
39 }
40
41 public byte[] toByteArray() {
42 return digest();
43 }
44}