1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.maven.shared.repository.utils; |
20 | |
|
21 | |
import java.io.File; |
22 | |
import java.io.FileInputStream; |
23 | |
import java.io.IOException; |
24 | |
import java.io.InputStream; |
25 | |
import java.security.MessageDigest; |
26 | |
import java.security.NoSuchAlgorithmException; |
27 | |
import java.util.regex.Matcher; |
28 | |
import java.util.regex.Pattern; |
29 | |
|
30 | |
import org.codehaus.plexus.util.IOUtil; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public final class DigestUtils |
39 | |
{ |
40 | |
private static final int CHECKSUM_BUFFER_SIZE = 16384; |
41 | |
|
42 | |
private static final int BYTE_MASK = 0xFF; |
43 | |
|
44 | |
private DigestUtils() |
45 | 0 | { |
46 | 0 | } |
47 | |
|
48 | |
public static String createChecksum( File file, String algorithm ) |
49 | |
throws IOException, NoSuchAlgorithmException |
50 | |
{ |
51 | 24 | MessageDigest digest = MessageDigest.getInstance( algorithm ); |
52 | |
|
53 | 24 | InputStream fis = new FileInputStream( file ); |
54 | |
try |
55 | |
{ |
56 | 24 | byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE]; |
57 | |
int numRead; |
58 | |
do |
59 | |
{ |
60 | 48 | numRead = fis.read( buffer ); |
61 | 48 | if ( numRead > 0 ) |
62 | |
{ |
63 | 24 | digest.update( buffer, 0, numRead ); |
64 | |
} |
65 | |
} |
66 | 48 | while ( numRead != -1 ); |
67 | |
} |
68 | |
finally |
69 | |
{ |
70 | 24 | IOUtil.close( fis ); |
71 | 24 | } |
72 | |
|
73 | 24 | return byteArrayToHexStr( digest.digest() ); |
74 | |
} |
75 | |
|
76 | |
public boolean verifyChecksum( File file, String checksum, String algorithm ) |
77 | |
throws NoSuchAlgorithmException, IOException |
78 | |
{ |
79 | 0 | boolean result = true; |
80 | |
|
81 | 0 | String trimmedChecksum = checksum.replace( '\n', ' ' ).trim(); |
82 | |
|
83 | 0 | Matcher m = Pattern.compile( algorithm.replaceAll( "-", "" ) + "\\s*\\((.*?)\\)\\s*=\\s*([a-zA-Z0-9]+)" ) |
84 | |
.matcher( trimmedChecksum ); |
85 | 0 | if ( m.matches() ) |
86 | |
{ |
87 | 0 | String filename = m.group( 1 ); |
88 | 0 | if ( !filename.equals( file.getName() ) ) |
89 | |
{ |
90 | |
|
91 | 0 | result = false; |
92 | |
} |
93 | 0 | trimmedChecksum = m.group( 2 ); |
94 | 0 | } |
95 | |
else |
96 | |
{ |
97 | |
|
98 | 0 | m = Pattern.compile( "([a-zA-Z0-9]+)\\s\\*?(.+)" ).matcher( trimmedChecksum ); |
99 | 0 | if ( m.matches() ) |
100 | |
{ |
101 | 0 | String filename = m.group( 2 ); |
102 | 0 | if ( !filename.equals( file.getName() ) ) |
103 | |
{ |
104 | |
|
105 | 0 | result = false; |
106 | |
} |
107 | 0 | trimmedChecksum = m.group( 1 ); |
108 | |
} |
109 | |
} |
110 | |
|
111 | 0 | if ( result ) |
112 | |
{ |
113 | |
|
114 | 0 | String sum = createChecksum( file, algorithm ); |
115 | 0 | result = trimmedChecksum.toUpperCase().equals( sum.toUpperCase() ); |
116 | |
} |
117 | 0 | return result; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
private static String byteArrayToHexStr( byte[] data ) |
127 | |
{ |
128 | 24 | String output = ""; |
129 | |
|
130 | 456 | for ( int cnt = 0; cnt < data.length; cnt++ ) |
131 | |
{ |
132 | |
|
133 | 432 | int tempInt = data[cnt] & BYTE_MASK; |
134 | |
|
135 | |
|
136 | 432 | String tempStr = Integer.toHexString( tempInt ); |
137 | |
|
138 | |
|
139 | 432 | if ( tempStr.length() == 1 ) |
140 | |
{ |
141 | 42 | tempStr = "0" + tempStr; |
142 | |
} |
143 | |
|
144 | |
|
145 | 432 | output = output + tempStr; |
146 | |
} |
147 | |
|
148 | 24 | return output.toUpperCase(); |
149 | |
} |
150 | |
} |