Coverage Report - org.codehaus.plexus.digest.DigestUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
DigestUtils
90 %
18/20
100 %
12/12
2,75
 
 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  0
     {
 31  
         // don't create this class
 32  0
     }
 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  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  
         // Free-BSD / openssl
 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  
             // GNU tools
 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  
 }