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 {
31
32 }
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 return cleanChecksum( rawChecksum, digester.getAlgorithm(), expectedFilename );
46 }
47
48 public static String cleanChecksum( String checksum, String algorithm, String path )
49 throws DigesterException
50 {
51 String trimmedChecksum = checksum.replace( '\n', ' ' ).trim();
52
53
54 String regex = algorithm.replaceAll( "-", "" ) + "\\s*\\((.*?)\\)\\s*=\\s*([a-fA-F0-9]+)";
55 Matcher m = Pattern.compile( regex ).matcher( trimmedChecksum );
56 if ( m.matches() )
57 {
58 String filename = m.group( 1 );
59 if ( !isValidChecksumPattern( filename, path ) )
60 {
61 throw new DigesterException( "Supplied checksum does not match checksum pattern" );
62 }
63 trimmedChecksum = m.group( 2 );
64 }
65 else
66 {
67
68 m = Pattern.compile( "([a-fA-F0-9]+)\\s+\\*?(.+)" ).matcher( trimmedChecksum );
69 if ( m.matches() )
70 {
71 String filename = m.group( 2 );
72 if ( !isValidChecksumPattern( filename, path ) )
73 {
74 throw new DigesterException( "Supplied checksum does not match checksum pattern" );
75 }
76 trimmedChecksum = m.group( 1 );
77 }
78 }
79 return trimmedChecksum;
80 }
81
82 private static boolean isValidChecksumPattern( String filename, String path )
83 {
84 return filename.endsWith( path ) || ( "-".equals( filename ) );
85 }
86 }