|
8 | 8 | import java.io.FileInputStream; |
9 | 9 | import java.io.IOException; |
10 | 10 | import java.io.InputStream; |
11 | | -import java.math.BigInteger; |
12 | | -import java.nio.charset.StandardCharsets; |
13 | | -import java.security.MessageDigest; |
14 | | -import java.security.NoSuchAlgorithmException; |
15 | 11 | import java.util.Arrays; |
| 12 | +import java.util.Iterator; |
16 | 13 | import java.util.Locale; |
17 | 14 |
|
18 | | -public enum HashFunction { |
19 | | - MD5 ("md5", 32), |
20 | | - SHA1 ("SHA-1", 40), |
21 | | - SHA256("SHA-256", 64), |
22 | | - SHA512("SHA-512", 128); |
23 | | - |
24 | | - private final String algo; |
25 | | - private final String pad; |
26 | | - private final String ext; |
27 | | - private Boolean supported; |
28 | | - |
29 | | - HashFunction(String algo, int length) { |
30 | | - this.algo = algo; |
31 | | - this.pad = String.format(Locale.ENGLISH, "%0" + length + "d", 0); |
32 | | - this.ext = this.name().toLowerCase(Locale.ENGLISH); |
33 | | - } |
| 15 | +public abstract class HashFunction implements Iterable<HashFunction> { |
| 16 | + public abstract String extension(); |
34 | 17 |
|
35 | | - public String extension() { |
36 | | - return this.ext; |
37 | | - } |
| 18 | + /* |
| 19 | + * Retreives a new instance that can be used for incremental hashing. |
| 20 | + */ |
| 21 | + public abstract HashInstance instance(); |
38 | 22 |
|
39 | | - public static HashFunction find(String name) { |
40 | | - String cleaned = name.toUpperCase(Locale.ENGLISH); |
41 | | - for (HashFunction func : values()) { |
42 | | - if (cleaned.equals(func.name())) |
43 | | - return func; |
| 23 | + public final String hash(File file) throws IOException { |
| 24 | + try (FileInputStream fin = new FileInputStream(file)) { |
| 25 | + return hash(fin); |
44 | 26 | } |
| 27 | + } |
45 | 28 |
|
46 | | - return null; |
| 29 | + public final String hash(File... files) throws IOException { |
| 30 | + return hash(Arrays.asList(files)); |
47 | 31 | } |
48 | 32 |
|
49 | | - public static HashFunction findByHash(String hash) { |
50 | | - int len = hash.length(); |
51 | | - for (HashFunction func : values()) { |
52 | | - if (func.pad.length() == len) |
53 | | - return func; |
54 | | - } |
| 33 | + public abstract String hash(Iterable<File> files) throws IOException; |
55 | 34 |
|
56 | | - return null; |
| 35 | + public final String hash(String data) { |
| 36 | + return hash(data == null ? new byte[0] : data.getBytes()); |
57 | 37 | } |
58 | 38 |
|
59 | | - public boolean supported() { |
60 | | - if (supported == null) { |
61 | | - try { |
62 | | - MessageDigest.getInstance(algo); |
63 | | - supported = true; |
64 | | - } catch (NoSuchAlgorithmException e) { |
65 | | - supported = false; |
66 | | - } |
67 | | - } |
68 | | - return supported; |
69 | | - } |
| 39 | + public abstract String hash(InputStream inputStream) throws IOException; |
| 40 | + public abstract String hash(byte[] bytes); |
70 | 41 |
|
71 | | - public MessageDigest get() { |
72 | | - try { |
73 | | - return MessageDigest.getInstance(algo); |
74 | | - } catch (NoSuchAlgorithmException e) { |
75 | | - throw new RuntimeException(e); |
76 | | - } |
77 | | - } |
| 42 | + public abstract String pad(String hash); |
78 | 43 |
|
79 | | - public String hash(File file) throws IOException { |
80 | | - try (FileInputStream fin = new FileInputStream(file)) { |
81 | | - return hash(fin); |
82 | | - } |
| 44 | + public static HashFunction alder32() { |
| 45 | + return Adler32.INSTANCE; |
83 | 46 | } |
84 | 47 |
|
85 | | - public String sneakyHash(File file) { |
86 | | - try { |
87 | | - return hash(file); |
88 | | - } catch (IOException e) { |
89 | | - return HashUtils.sneak(e); |
90 | | - } |
| 48 | + public static HashFunction crc32() { |
| 49 | + return CRC32.INSTANCE; |
91 | 50 | } |
92 | 51 |
|
93 | | - public String hash(File... files) throws IOException { |
94 | | - return hash(Arrays.asList(files)); |
| 52 | + public static HashFunction md5() { |
| 53 | + return MD5.INSTANCE; |
95 | 54 | } |
96 | 55 |
|
97 | | - public String sneakyHash(File... files) { |
98 | | - try { |
99 | | - return hash(Arrays.asList(files)); |
100 | | - } catch (IOException e) { |
101 | | - return HashUtils.sneak(e); |
102 | | - } |
| 56 | + public static HashFunction sha1() { |
| 57 | + return SHA1.INSTANCE; |
103 | 58 | } |
104 | 59 |
|
105 | | - public String hash(Iterable<File> files) throws IOException { |
106 | | - MessageDigest hash = get(); |
107 | | - byte[] buf = new byte[1024]; |
108 | | - |
109 | | - for (File file : files) { |
110 | | - if (!file.exists()) |
111 | | - continue; |
112 | | - |
113 | | - try (FileInputStream fin = new FileInputStream(file)) { |
114 | | - int count = -1; |
115 | | - while ((count = fin.read(buf)) != -1) |
116 | | - hash.update(buf, 0, count); |
117 | | - } |
118 | | - } |
119 | | - return pad(new BigInteger(1, hash.digest()).toString(16)); |
| 60 | + public static HashFunction sha256() { |
| 61 | + return SHA256.INSTANCE; |
120 | 62 | } |
121 | 63 |
|
122 | | - public String hash(String data) { |
123 | | - return hash(data == null ? new byte[0] : data.getBytes(StandardCharsets.UTF_8)); |
| 64 | + public static HashFunction sha512() { |
| 65 | + return SHA512.INSTANCE; |
124 | 66 | } |
125 | 67 |
|
126 | | - public String hash(InputStream stream) throws IOException { |
127 | | - MessageDigest hash = get(); |
128 | | - byte[] buf = new byte[1024]; |
129 | | - int count = -1; |
130 | | - while ((count = stream.read(buf)) != -1) |
131 | | - hash.update(buf, 0, count); |
132 | | - return pad(new BigInteger(1, hash.digest()).toString(16)); |
| 68 | + public static HashFunction byName(String extension) { |
| 69 | + switch (extension.toLowerCase(Locale.ROOT)) { |
| 70 | + case "adler32": return Adler32.INSTANCE; |
| 71 | + case "crc32": return CRC32.INSTANCE; |
| 72 | + case "md5": return MD5.INSTANCE; |
| 73 | + case "sha1": return SHA1.INSTANCE; |
| 74 | + case "sha256": return SHA256.INSTANCE; |
| 75 | + case "sha512": return SHA512.INSTANCE; |
| 76 | + default: throw new UnsupportedOperationException("Unknown hash extension: " + extension); |
| 77 | + } |
133 | 78 | } |
134 | 79 |
|
135 | | - public String hash(byte[] data) { |
136 | | - return pad(new BigInteger(1, get().digest(data)).toString(16)); |
| 80 | + public static HashFunction[] values() { |
| 81 | + return new HashFunction[] { Adler32.INSTANCE, CRC32.INSTANCE, MD5.INSTANCE, SHA1.INSTANCE, SHA256.INSTANCE, SHA512.INSTANCE }; |
137 | 82 | } |
138 | 83 |
|
139 | | - public String pad(String hash) { |
140 | | - return (pad + hash).substring(hash.length()); |
141 | | - } |
| 84 | + @Override |
| 85 | + public Iterator<HashFunction> iterator() { |
| 86 | + return new Iterator<HashFunction>() { |
| 87 | + private final HashFunction[] values = values(); |
| 88 | + private int index = 0; |
142 | 89 |
|
143 | | - private static final byte[] HEX_ARRAY = "0123456789abcdef".getBytes(StandardCharsets.UTF_8); |
144 | | - public static String bytesToHex(byte[] bytes) { |
145 | | - if (bytes == null || bytes.length == 0) |
146 | | - return ""; |
147 | | - byte[] hexChars = new byte[bytes.length * 3 - 1]; |
148 | | - for (int j = 0, k = 0; j < bytes.length; j++) { |
149 | | - int v = bytes[j] & 0xFF; |
150 | | - hexChars[k++] = HEX_ARRAY[v >>> 4]; |
151 | | - hexChars[k++] = HEX_ARRAY[v & 0x0F]; |
152 | | - if (j < bytes.length - 1) |
153 | | - hexChars[k++] = ' '; |
154 | | - } |
155 | | - return new String(hexChars, StandardCharsets.UTF_8); |
| 90 | + @Override |
| 91 | + public boolean hasNext() { |
| 92 | + return values.length > index; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public HashFunction next() { |
| 97 | + return values[index++]; |
| 98 | + } |
| 99 | + }; |
156 | 100 | } |
157 | 101 | } |
0 commit comments