1 | |
package org.codehaus.plexus.digest; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import java.util.regex.Matcher; |
20 | |
import java.util.regex.Pattern; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class DigestUtils |
28 | |
{ |
29 | |
private DigestUtils() |
30 | 0 | { |
31 | |
|
32 | 0 | } |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public static String cleanChecksum( String rawChecksum, Digester digester, String expectedFilename ) throws DigesterException |
44 | |
{ |
45 | 12 | return cleanChecksum( rawChecksum, digester.getAlgorithm(), expectedFilename ); |
46 | |
} |
47 | |
|
48 | |
public static String cleanChecksum( String checksum, String algorithm, String path ) |
49 | |
throws DigesterException |
50 | |
{ |
51 | 31 | String trimmedChecksum = checksum.replace( '\n', ' ' ).trim(); |
52 | |
|
53 | |
|
54 | 31 | String regex = algorithm.replaceAll( "-", "" ) + "\\s*\\((.*?)\\)\\s*=\\s*([a-fA-F0-9]+)"; |
55 | 31 | Matcher m = Pattern.compile( regex ).matcher( trimmedChecksum ); |
56 | 31 | if ( m.matches() ) |
57 | |
{ |
58 | 9 | String filename = m.group( 1 ); |
59 | 9 | if ( !isValidChecksumPattern( filename, path ) ) |
60 | |
{ |
61 | 1 | throw new DigesterException( "Supplied checksum does not match checksum pattern" ); |
62 | |
} |
63 | 8 | trimmedChecksum = m.group( 2 ); |
64 | 8 | } |
65 | |
else |
66 | |
{ |
67 | |
|
68 | 22 | m = Pattern.compile( "([a-fA-F0-9]+)\\s+\\*?(.+)" ).matcher( trimmedChecksum ); |
69 | 22 | if ( m.matches() ) |
70 | |
{ |
71 | 17 | String filename = m.group( 2 ); |
72 | 17 | if ( !isValidChecksumPattern( filename, path ) ) |
73 | |
{ |
74 | 1 | throw new DigesterException( "Supplied checksum does not match checksum pattern" ); |
75 | |
} |
76 | 16 | trimmedChecksum = m.group( 1 ); |
77 | |
} |
78 | |
} |
79 | 29 | return trimmedChecksum; |
80 | |
} |
81 | |
|
82 | |
private static boolean isValidChecksumPattern( String filename, String path ) |
83 | |
{ |
84 | 26 | return filename.endsWith( path ) || ( "-".equals( filename ) ); |
85 | |
} |
86 | |
} |