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(
59 new FileInputStream( checksumFile ), StandardCharsets.UTF_8 ), 512 ) )
60 {
61 while ( true )
62 {
63 String line = br.readLine();
64 if ( line == null )
65 {
66 break;
67 }
68 line = line.trim();
69 if ( line.length() > 0 )
70 {
71 checksum = line;
72 break;
73 }
74 }
75 }
76
77 if ( checksum.matches( ".+= [0-9A-Fa-f]+" ) )
78 {
79 int lastSpacePos = checksum.lastIndexOf( ' ' );
80 checksum = checksum.substring( lastSpacePos + 1 );
81 }
82 else
83 {
84 int spacePos = checksum.indexOf( ' ' );
85
86 if ( spacePos != -1 )
87 {
88 checksum = checksum.substring( 0, spacePos );
89 }
90 }
91
92 return checksum;
93 }
94
95
96
97
98
99
100
101
102
103
104
105 public static Map<String, Object> calc( File dataFile, Collection<String> algos )
106 throws IOException
107 {
108 return calc( new FileInputStream( dataFile ), algos );
109 }
110
111
112 public static Map<String, Object> calc( byte[] dataBytes, Collection<String> algos )
113 throws IOException
114 {
115 return calc( new ByteArrayInputStream( dataBytes ), algos );
116 }
117
118
119 private static Map<String, Object> calc( InputStream data, Collection<String> algos )
120 throws IOException
121 {
122 Map<String, Object> results = new LinkedHashMap<>();
123
124 Map<String, MessageDigest> digests = new LinkedHashMap<>();
125 for ( String algo : algos )
126 {
127 try
128 {
129 digests.put( algo, MessageDigest.getInstance( algo ) );
130 }
131 catch ( NoSuchAlgorithmException e )
132 {
133 results.put( algo, e );
134 }
135 }
136
137 try ( InputStream in = data )
138 {
139 for ( byte[] buffer = new byte[ 32 * 1024 ];; )
140 {
141 int read = in.read( buffer );
142 if ( read < 0 )
143 {
144 break;
145 }
146 for ( MessageDigest digest : digests.values() )
147 {
148 digest.update( buffer, 0, read );
149 }
150 }
151 }
152
153 for ( Map.Entry<String, MessageDigest> entry : digests.entrySet() )
154 {
155 byte[] bytes = entry.getValue().digest();
156
157 results.put( entry.getKey(), toHexString( bytes ) );
158 }
159
160 return results;
161 }
162
163
164
165
166
167
168
169
170
171 @SuppressWarnings( "checkstyle:magicnumber" )
172 public static String toHexString( byte[] bytes )
173 {
174 if ( bytes == null )
175 {
176 return null;
177 }
178
179 StringBuilder buffer = new StringBuilder( bytes.length * 2 );
180
181 for ( byte aByte : bytes )
182 {
183 int b = aByte & 0xFF;
184 if ( b < 0x10 )
185 {
186 buffer.append( '0' );
187 }
188 buffer.append( Integer.toHexString( b ) );
189 }
190
191 return buffer.toString();
192 }
193
194 }