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