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.lang.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.file.Files;
31 import java.nio.file.StandardOpenOption;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
42 * <dt>Checksum File</dt>
43 * <dd>The file that contains the previously calculated checksum value for the reference file.
44 * This is a text file with the extension ".sha1" or ".md5", and contains a single entry
45 * consisting of an optional reference filename, and a checksum string.
47 * <dt>Reference File</dt>
48 * <dd>The file that is being referenced in the checksum file.</dd>
51 public class ChecksummedFile
53 private final Logger log = LoggerFactory.getLogger( ChecksummedFile.class );
55 private static final Pattern METADATA_PATTERN = Pattern.compile( "maven-metadata-\\S*.xml" );
57 private final File referenceFile;
60 * Construct a ChecksummedFile object.
62 * @param referenceFile
64 public ChecksummedFile( final File referenceFile )
66 this.referenceFile = referenceFile;
70 * Calculate the checksum based on a given checksum.
72 * @param checksumAlgorithm the algorithm to use.
73 * @return the checksum string for the file.
74 * @throws IOException if unable to calculate the checksum.
76 public String calculateChecksum( ChecksumAlgorithm checksumAlgorithm )
80 try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
82 Checksum checksum = new Checksum( checksumAlgorithm );
83 checksum.update( fis );
84 return checksum.getChecksum();
89 * Creates a checksum file of the provided referenceFile.
91 * @param checksumAlgorithm the hash to use.
92 * @return the checksum File that was created.
93 * @throws IOException if there was a problem either reading the referenceFile, or writing the checksum file.
95 public File createChecksum( ChecksumAlgorithm checksumAlgorithm )
98 File checksumFile = new File( referenceFile.getAbsolutePath() + "." + checksumAlgorithm.getExt() );
99 Files.deleteIfExists( checksumFile.toPath() );
100 String checksum = calculateChecksum( checksumAlgorithm );
101 Files.write( checksumFile.toPath(), //
102 ( checksum + " " + referenceFile.getName() ).getBytes(), //
103 StandardOpenOption.CREATE_NEW );
108 * Get the checksum file for the reference file and hash.
110 * @param checksumAlgorithm the hash that we are interested in.
111 * @return the checksum file to return
113 public File getChecksumFile( ChecksumAlgorithm checksumAlgorithm )
115 return new File( referenceFile.getAbsolutePath() + "." + checksumAlgorithm.getExt() );
120 * Given a checksum file, check to see if the file it represents is valid according to the checksum.
124 * NOTE: Only supports single file checksums of type MD5 or SHA1.
127 * @param algorithm the algorithms to check for.
128 * @return true if the checksum is valid for the file it represents. or if the checksum file does not exist.
129 * @throws IOException if the reading of the checksumFile or the file it refers to fails.
131 public boolean isValidChecksum( ChecksumAlgorithm algorithm )
134 return isValidChecksums( new ChecksumAlgorithm[]{ algorithm } );
138 * Of any checksum files present, validate that the reference file conforms
139 * the to the checksum.
141 * @param algorithms the algorithms to check for.
142 * @return true if the checksums report that the the reference file is valid, false if invalid.
144 public boolean isValidChecksums( ChecksumAlgorithm algorithms[] )
147 try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
149 List<Checksum> checksums = new ArrayList<>( algorithms.length );
150 // Create checksum object for each algorithm.
151 for ( ChecksumAlgorithm checksumAlgorithm : algorithms )
153 File checksumFile = getChecksumFile( checksumAlgorithm );
155 // Only add algorithm if checksum file exists.
156 if ( checksumFile.exists() )
158 checksums.add( new Checksum( checksumAlgorithm ) );
163 if ( checksums.isEmpty() )
165 // No checksum objects, no checksum files, default to is invalid.
169 // Parse file once, for all checksums.
172 Checksum.update( checksums, fis );
174 catch ( IOException e )
176 log.warn( "Unable to update checksum:{}", e.getMessage() );
180 boolean valid = true;
182 // check the checksum files
185 for ( Checksum checksum : checksums )
187 ChecksumAlgorithm checksumAlgorithm = checksum.getAlgorithm();
188 File checksumFile = getChecksumFile( checksumAlgorithm );
190 String rawChecksum = FileUtils.readFileToString( checksumFile );
191 String expectedChecksum = parseChecksum( rawChecksum, checksumAlgorithm, referenceFile.getName() );
193 if ( !StringUtils.equalsIgnoreCase( expectedChecksum, checksum.getChecksum() ) )
199 catch ( IOException e )
201 log.warn( "Unable to read / parse checksum: {}", e.getMessage() );
207 catch ( IOException e )
209 log.warn( "Unable to read / parse checksum: {}", e.getMessage() );
215 * Fix or create checksum files for the reference file.
217 * @param algorithms the hashes to check for.
218 * @return true if checksums were created successfully.
220 public boolean fixChecksums( ChecksumAlgorithm[] algorithms )
222 List<Checksum> checksums = new ArrayList<>( algorithms.length );
223 // Create checksum object for each algorithm.
224 for ( ChecksumAlgorithm checksumAlgorithm : algorithms )
226 checksums.add( new Checksum( checksumAlgorithm ) );
230 if ( checksums.isEmpty() )
232 // No checksum objects, no checksum files, default to is valid.
236 try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
238 // Parse file once, for all checksums.
239 Checksum.update( checksums, fis );
241 catch ( IOException e )
243 log.warn( e.getMessage(), e );
247 boolean valid = true;
249 // check the hash files
250 for ( Checksum checksum : checksums )
252 ChecksumAlgorithm checksumAlgorithm = checksum.getAlgorithm();
255 File checksumFile = getChecksumFile( checksumAlgorithm );
256 String actualChecksum = checksum.getChecksum();
258 if ( checksumFile.exists() )
260 String rawChecksum = FileUtils.readFileToString( checksumFile );
261 String expectedChecksum = parseChecksum( rawChecksum, checksumAlgorithm, referenceFile.getName() );
263 if ( !StringUtils.equalsIgnoreCase( expectedChecksum, actualChecksum ) )
265 // create checksum (again)
266 FileUtils.writeStringToFile( checksumFile, actualChecksum + " " + referenceFile.getName() );
271 FileUtils.writeStringToFile( checksumFile, actualChecksum + " " + referenceFile.getName() );
274 catch ( IOException e )
276 log.warn( e.getMessage(), e );
285 private boolean isValidChecksumPattern( String filename, String path )
287 // check if it is a remote metadata file
289 Matcher m = METADATA_PATTERN.matcher( path );
292 return filename.endsWith( path ) || ( "-".equals( filename ) ) || filename.endsWith( "maven-metadata.xml" );
295 return filename.endsWith( path ) || ( "-".equals( filename ) );
299 * Parse a checksum string.
301 * Validate the expected path, and expected checksum algorithm, then return
302 * the trimmed checksum hex string.
305 * @param rawChecksumString
306 * @param expectedHash
307 * @param expectedPath
309 * @throws IOException
311 public String parseChecksum( String rawChecksumString, ChecksumAlgorithm expectedHash, String expectedPath )
314 String trimmedChecksum = rawChecksumString.replace( '\n', ' ' ).trim();
316 // Free-BSD / openssl
317 String regex = expectedHash.getType() + "\\s*\\(([^)]*)\\)\\s*=\\s*([a-fA-F0-9]+)";
318 Matcher m = Pattern.compile( regex ).matcher( trimmedChecksum );
321 String filename = m.group( 1 );
322 if ( !isValidChecksumPattern( filename, expectedPath ) )
324 throw new IOException(
325 "Supplied checksum file '" + filename + "' does not match expected file: '" + expectedPath + "'" );
327 trimmedChecksum = m.group( 2 );
332 m = Pattern.compile( "([a-fA-F0-9]+)\\s+\\*?(.+)" ).matcher( trimmedChecksum );
335 String filename = m.group( 2 );
336 if ( !isValidChecksumPattern( filename, expectedPath ) )
338 throw new IOException(
339 "Supplied checksum file '" + filename + "' does not match expected file: '" + expectedPath
342 trimmedChecksum = m.group( 1 );
345 return trimmedChecksum;