1 package org.apache.archiva.checksum;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
40 * <lh>Terminology:</lh>
41 * <dt>Checksum File</dt>
42 * <dd>The file that contains the previously calculated checksum value for the reference file.
43 * This is a text file with the extension ".sha1" or ".md5", and contains a single entry
44 * consisting of an optional reference filename, and a checksum string.
46 * <dt>Reference File</dt>
47 * <dd>The file that is being referenced in the checksum file.</dd>
52 public class ChecksummedFile
54 private Logger log = LoggerFactory.getLogger( ChecksummedFile.class );
56 private final File referenceFile;
59 * Construct a ChecksummedFile object.
61 * @param referenceFile
63 public ChecksummedFile( final File referenceFile )
65 this.referenceFile = referenceFile;
69 * Calculate the checksum based on a given checksum.
71 * @param checksumAlgorithm the algorithm to use.
72 * @return the checksum string for the file.
73 * @throws IOException if unable to calculate the checksum.
75 public String calculateChecksum( ChecksumAlgorithm checksumAlgorithm )
78 FileInputStream fis = null;
81 Checksum checksum = new Checksum( checksumAlgorithm );
82 fis = new FileInputStream( referenceFile );
83 checksum.update( fis );
84 return checksum.getChecksum();
88 IOUtils.closeQuietly( fis );
93 * Creates a checksum file of the provided referenceFile.
95 * @param checksumAlgorithm the hash to use.
96 * @return the checksum File that was created.
97 * @throws IOException if there was a problem either reading the referenceFile, or writing the checksum file.
99 public File createChecksum( ChecksumAlgorithm checksumAlgorithm )
102 File checksumFile = new File( referenceFile.getAbsolutePath() + "." + checksumAlgorithm.getExt() );
103 String checksum = calculateChecksum( checksumAlgorithm );
104 FileUtils.writeStringToFile( checksumFile, checksum + " " + referenceFile.getName() );
109 * Get the checksum file for the reference file and hash.
111 * @param checksumAlgorithm the hash that we are interested in.
112 * @return the checksum file to return
114 public File getChecksumFile( ChecksumAlgorithm checksumAlgorithm )
116 return new File( referenceFile.getAbsolutePath() + "." + checksumAlgorithm.getExt() );
121 * Given a checksum file, check to see if the file it represents is valid according to the checksum.
125 * NOTE: Only supports single file checksums of type MD5 or SHA1.
128 * @param checksumFile the algorithms to check for.
129 * @return true if the checksum is valid for the file it represents. or if the checksum file does not exist.
130 * @throws IOException if the reading of the checksumFile or the file it refers to fails.
132 public boolean isValidChecksum( ChecksumAlgorithm algorithm )
135 return isValidChecksums( new ChecksumAlgorithm[]{ algorithm } );
139 * Of any checksum files present, validate that the reference file conforms
140 * the to the checksum.
142 * @param algorithms the algorithms to check for.
143 * @return true if the checksums report that the the reference file is valid, false if invalid.
145 public boolean isValidChecksums( ChecksumAlgorithm algorithms[] )
147 FileInputStream fis = null;
150 List<Checksum> checksums = new ArrayList<Checksum>( algorithms.length );
151 // Create checksum object for each algorithm.
152 for ( ChecksumAlgorithm checksumAlgorithm : algorithms )
154 File checksumFile = getChecksumFile( checksumAlgorithm );
156 // Only add algorithm if checksum file exists.
157 if ( checksumFile.exists() )
159 checksums.add( new Checksum( checksumAlgorithm ) );
164 if ( checksums.isEmpty() )
166 // No checksum objects, no checksum files, default to is invalid.
170 // Parse file once, for all checksums.
173 fis = new FileInputStream( referenceFile );
174 Checksum.update( checksums, fis );
176 catch ( IOException e )
178 log.warn( "Unable to update checksum:" + e.getMessage() );
182 boolean valid = true;
184 // check the checksum files
187 for ( Checksum checksum : checksums )
189 ChecksumAlgorithm checksumAlgorithm = checksum.getAlgorithm();
190 File checksumFile = getChecksumFile( checksumAlgorithm );
192 String rawChecksum = FileUtils.readFileToString( checksumFile );
193 String expectedChecksum = parseChecksum( rawChecksum, checksumAlgorithm, referenceFile.getName() );
195 if ( StringUtils.equalsIgnoreCase( expectedChecksum, checksum.getChecksum() ) == false )
201 catch ( IOException e )
203 log.warn( "Unable to read / parse checksum: " + e.getMessage() );
211 IOUtils.closeQuietly( fis );
216 * Fix or create checksum files for the reference file.
218 * @param algorithms the hashes to check for.
219 * @return true if checksums were created successfully.
221 public boolean fixChecksums( ChecksumAlgorithm[] algorithms )
223 List<Checksum> checksums = new ArrayList<Checksum>( algorithms.length );
224 // Create checksum object for each algorithm.
225 for ( ChecksumAlgorithm checksumAlgorithm : algorithms )
227 checksums.add( new Checksum( checksumAlgorithm ) );
231 if ( checksums.isEmpty() )
233 // No checksum objects, no checksum files, default to is valid.
237 FileInputStream fis = null;
240 // Parse file once, for all checksums.
241 fis = new FileInputStream( referenceFile );
242 Checksum.update( checksums, fis );
244 catch ( IOException e )
246 log.warn( e.getMessage(), e );
251 IOUtils.closeQuietly( fis );
254 boolean valid = true;
256 // check the hash files
257 for ( Checksum checksum : checksums )
259 ChecksumAlgorithm checksumAlgorithm = checksum.getAlgorithm();
262 File checksumFile = getChecksumFile( checksumAlgorithm );
263 String actualChecksum = checksum.getChecksum();
265 if ( checksumFile.exists() )
267 String rawChecksum = FileUtils.readFileToString( checksumFile );
268 String expectedChecksum = parseChecksum( rawChecksum, checksumAlgorithm, referenceFile.getName() );
270 if ( !StringUtils.equalsIgnoreCase( expectedChecksum, actualChecksum ) )
272 // create checksum (again)
273 FileUtils.writeStringToFile( checksumFile, actualChecksum + " " + referenceFile.getName() );
278 FileUtils.writeStringToFile( checksumFile, actualChecksum + " " + referenceFile.getName() );
281 catch ( IOException e )
283 log.warn( e.getMessage(), e );
292 private boolean isValidChecksumPattern( String filename, String path )
294 // check if it is a remote metadata file
295 Pattern pattern = Pattern.compile( "maven-metadata-\\S*.xml" );
296 Matcher m = pattern.matcher( path );
299 return filename.endsWith( path ) || ( "-".equals( filename ) ) || filename.endsWith( "maven-metadata.xml" );
302 return filename.endsWith( path ) || ( "-".equals( filename ) );
306 * Parse a checksum string.
308 * Validate the expected path, and expected checksum algorithm, then return
309 * the trimmed checksum hex string.
311 * @param rawChecksumString
312 * @param expectedHash
313 * @param expectedPath
315 * @throws IOException
317 public String parseChecksum( String rawChecksumString, ChecksumAlgorithm expectedHash, String expectedPath )
320 String trimmedChecksum = rawChecksumString.replace( '\n', ' ' ).trim();
322 // Free-BSD / openssl
323 String regex = expectedHash.getType() + "\\s*\\(([^)]*)\\)\\s*=\\s*([a-fA-F0-9]+)";
324 Matcher m = Pattern.compile( regex ).matcher( trimmedChecksum );
327 String filename = m.group( 1 );
328 if ( !isValidChecksumPattern( filename, expectedPath ) )
330 throw new IOException(
331 "Supplied checksum file '" + filename + "' does not match expected file: '" + expectedPath + "'" );
333 trimmedChecksum = m.group( 2 );
338 m = Pattern.compile( "([a-fA-F0-9]+)\\s+\\*?(.+)" ).matcher( trimmedChecksum );
341 String filename = m.group( 2 );
342 if ( !isValidChecksumPattern( filename, expectedPath ) )
344 throw new IOException(
345 "Supplied checksum file '" + filename + "' does not match expected file: '" + expectedPath
348 trimmedChecksum = m.group( 1 );
351 return trimmedChecksum;