1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.index.packer;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.charset.StandardCharsets;
28 import java.security.MessageDigest;
29 import java.security.NoSuchAlgorithmException;
30
31
32
33
34
35
36 public class DigesterUtils {
37
38
39
40
41
42
43
44
45 private static String getDigest(String alg, InputStream is) throws NoSuchAlgorithmException {
46 String result;
47
48 try {
49 try {
50 byte[] buffer = new byte[1024];
51
52 MessageDigest md = MessageDigest.getInstance(alg);
53
54 int numRead;
55
56 do {
57 numRead = is.read(buffer);
58
59 if (numRead > 0) {
60 md.update(buffer, 0, numRead);
61 }
62 } while (numRead != -1);
63
64 result = new String(encodeHex(md.digest()));
65 } finally {
66 is.close();
67 }
68 } catch (IOException e) {
69
70 result = null;
71 }
72
73 return result;
74 }
75
76
77
78
79
80
81
82
83
84 public static String getSha1Digest(String content) {
85 try {
86 InputStream fis = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
87
88 return getDigest("SHA1", fis);
89 } catch (NoSuchAlgorithmException e) {
90
91 return null;
92 }
93 }
94
95
96
97
98
99
100
101 public static String getSha1Digest(InputStream is) {
102 try {
103 return getDigest("SHA1", is);
104 } catch (NoSuchAlgorithmException e) {
105
106 return null;
107 }
108 }
109
110
111
112
113
114
115
116 public static String getSha1Digest(File file) throws IOException {
117 try (FileInputStream fis = new FileInputStream(file)) {
118 return getDigest("SHA1", fis);
119 } catch (NoSuchAlgorithmException | FileNotFoundException e) {
120
121 return null;
122 }
123 }
124
125
126
127
128
129
130
131
132
133 public static String getMd5Digest(String content) {
134 try {
135 InputStream fis = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
136
137 return getDigest("MD5", fis);
138 } catch (NoSuchAlgorithmException e) {
139
140 return null;
141 }
142 }
143
144
145
146
147
148
149
150 public static String getMd5Digest(InputStream is) {
151 try {
152 return getDigest("MD5", is);
153 } catch (NoSuchAlgorithmException e) {
154
155 return null;
156 }
157 }
158
159
160
161
162
163
164
165 public static String getMd5Digest(File file) throws IOException {
166
167 try (InputStream fis = new FileInputStream(file)) {
168 return getDigest("MD5", fis);
169 } catch (NoSuchAlgorithmException | FileNotFoundException e) {
170
171 return null;
172 }
173 }
174
175
176
177 private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
178 };
179
180
181
182
183
184
185
186 public static char[] encodeHex(byte[] data) {
187 int l = data.length;
188
189 char[] out = new char[l << 1];
190
191
192 for (int i = 0, j = 0; i < l; i++) {
193 out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
194 out[j++] = DIGITS[0x0F & data[i]];
195 }
196
197 return out;
198 }
199 }