Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame^] | 1 | package aQute.libg.cryptography; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | import aQute.lib.hex.*; |
| 6 | |
| 7 | public 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 | } |