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
39
40 @Deprecated
41 public final class ChecksumUtils {
42
43 private ChecksumUtils() {
44
45 }
46
47
48
49
50
51
52
53
54
55 @Deprecated
56 public static String read(File checksumFile) throws IOException {
57 String checksum = "";
58 try (BufferedReader br = new BufferedReader(
59 new InputStreamReader(new FileInputStream(checksumFile), StandardCharsets.UTF_8), 512)) {
60 while (true) {
61 String line = br.readLine();
62 if (line == null) {
63 break;
64 }
65 line = line.trim();
66 if (!line.isEmpty()) {
67 checksum = line;
68 break;
69 }
70 }
71 }
72
73 if (checksum.matches(".+= [0-9A-Fa-f]+")) {
74 int lastSpacePos = checksum.lastIndexOf(' ');
75 checksum = checksum.substring(lastSpacePos + 1);
76 } else {
77 int spacePos = checksum.indexOf(' ');
78
79 if (spacePos != -1) {
80 checksum = checksum.substring(0, spacePos);
81 }
82 }
83
84 return checksum;
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98 @Deprecated
99 public static Map<String, Object> calc(File dataFile, Collection<String> algos) throws IOException {
100 return calc(new FileInputStream(dataFile), algos);
101 }
102
103
104
105
106 @Deprecated
107 public static Map<String, Object> calc(byte[] dataBytes, Collection<String> algos) throws IOException {
108 return calc(new ByteArrayInputStream(dataBytes), algos);
109 }
110
111 private static Map<String, Object> calc(InputStream data, Collection<String> algos) throws IOException {
112 Map<String, Object> results = new LinkedHashMap<>();
113
114 Map<String, MessageDigest> digests = new LinkedHashMap<>();
115 for (String algo : algos) {
116 try {
117 digests.put(algo, MessageDigest.getInstance(algo));
118 } catch (NoSuchAlgorithmException e) {
119 results.put(algo, e);
120 }
121 }
122
123 try (InputStream in = data) {
124 for (byte[] buffer = new byte[32 * 1024]; ; ) {
125 int read = in.read(buffer);
126 if (read < 0) {
127 break;
128 }
129 for (MessageDigest digest : digests.values()) {
130 digest.update(buffer, 0, read);
131 }
132 }
133 }
134
135 for (Map.Entry<String, MessageDigest> entry : digests.entrySet()) {
136 byte[] bytes = entry.getValue().digest();
137
138 results.put(entry.getKey(), toHexString(bytes));
139 }
140
141 return results;
142 }
143
144
145
146
147
148
149
150
151 @SuppressWarnings("checkstyle:magicnumber")
152 public static String toHexString(byte[] bytes) {
153 return StringDigestUtil.toHexString(bytes);
154 }
155
156
157
158
159
160
161
162
163
164 @SuppressWarnings("checkstyle:magicnumber")
165 public static byte[] fromHexString(String hexString) {
166 return StringDigestUtil.fromHexString(hexString);
167 }
168 }