1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util;
20
21 import java.io.BufferedReader;
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.nio.charset.StandardCharsets;
29 import java.security.MessageDigest;
30 import java.security.NoSuchAlgorithmException;
31 import java.util.Collection;
32 import java.util.LinkedHashMap;
33 import java.util.Map;
34
35
36
37
38 public final class ChecksumUtils {
39
40 private ChecksumUtils() {
41
42 }
43
44
45
46
47
48
49
50
51
52 @Deprecated
53 public static String read(File checksumFile) throws IOException {
54 String checksum = "";
55 try (BufferedReader br = new BufferedReader(
56 new InputStreamReader(new FileInputStream(checksumFile), StandardCharsets.UTF_8), 512)) {
57 while (true) {
58 String line = br.readLine();
59 if (line == null) {
60 break;
61 }
62 line = line.trim();
63 if (line.length() > 0) {
64 checksum = line;
65 break;
66 }
67 }
68 }
69
70 if (checksum.matches(".+= [0-9A-Fa-f]+")) {
71 int lastSpacePos = checksum.lastIndexOf(' ');
72 checksum = checksum.substring(lastSpacePos + 1);
73 } else {
74 int spacePos = checksum.indexOf(' ');
75
76 if (spacePos != -1) {
77 checksum = checksum.substring(0, spacePos);
78 }
79 }
80
81 return checksum;
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95 @Deprecated
96 public static Map<String, Object> calc(File dataFile, Collection<String> algos) throws IOException {
97 return calc(new FileInputStream(dataFile), algos);
98 }
99
100
101
102
103 @Deprecated
104 public static Map<String, Object> calc(byte[] dataBytes, Collection<String> algos) throws IOException {
105 return calc(new ByteArrayInputStream(dataBytes), algos);
106 }
107
108 private static Map<String, Object> calc(InputStream data, Collection<String> algos) throws IOException {
109 Map<String, Object> results = new LinkedHashMap<>();
110
111 Map<String, MessageDigest> digests = new LinkedHashMap<>();
112 for (String algo : algos) {
113 try {
114 digests.put(algo, MessageDigest.getInstance(algo));
115 } catch (NoSuchAlgorithmException e) {
116 results.put(algo, e);
117 }
118 }
119
120 try (InputStream in = data) {
121 for (byte[] buffer = new byte[32 * 1024]; ; ) {
122 int read = in.read(buffer);
123 if (read < 0) {
124 break;
125 }
126 for (MessageDigest digest : digests.values()) {
127 digest.update(buffer, 0, read);
128 }
129 }
130 }
131
132 for (Map.Entry<String, MessageDigest> entry : digests.entrySet()) {
133 byte[] bytes = entry.getValue().digest();
134
135 results.put(entry.getKey(), toHexString(bytes));
136 }
137
138 return results;
139 }
140
141
142
143
144
145
146
147
148 @SuppressWarnings("checkstyle:magicnumber")
149 public static String toHexString(byte[] bytes) {
150 if (bytes == null) {
151 return null;
152 }
153
154 StringBuilder buffer = new StringBuilder(bytes.length * 2);
155
156 for (byte aByte : bytes) {
157 int b = aByte & 0xFF;
158 if (b < 0x10) {
159 buffer.append('0');
160 }
161 buffer.append(Integer.toHexString(b));
162 }
163
164 return buffer.toString();
165 }
166
167
168
169
170
171
172
173
174
175 @SuppressWarnings("checkstyle:magicnumber")
176 public static byte[] fromHexString(String hexString) {
177 if (hexString == null) {
178 return null;
179 }
180 if (hexString.isEmpty()) {
181 return new byte[] {};
182 }
183 int len = hexString.length();
184 if (len % 2 != 0) {
185 throw new IllegalArgumentException("hexString length not even");
186 }
187 byte[] data = new byte[len / 2];
188 for (int i = 0; i < len; i += 2) {
189 data[i / 2] = (byte)
190 ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
191 }
192 return data;
193 }
194 }