View Javadoc
1   package org.codehaus.plexus.digest;
2   
3   /*
4    * Copyright 2001-2006 The Codehaus.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  /**
23   * Parse files from checksum file formats.
24   *
25   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
26   */
27  public class DigestUtils
28  {
29      private DigestUtils()
30      {
31          // don't create this class
32      }
33      
34      /**
35       * Take a raw checksum string and verify that it matches the expectedFilename and digester, then
36       * return the trimmed checksum string.
37       * 
38       * @param rawChecksum the raw checksum string that may include the filename.
39       * @param digester the expected digester for this checksum string.
40       * @return the trimmed checksum string (no filename portion)
41       * @throws DigesterException if there was a problem verifying the checksum string.
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          // Free-BSD / openssl
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              // GNU tools
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  }