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.archiva.common.utils.FileUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.charset.Charset;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.StandardOpenOption;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.regex.Matcher;
36 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
54 private static Charset FILE_ENCODING = Charset.forName( "UTF-8" );
56 private final Logger log = LoggerFactory.getLogger( ChecksummedFile.class );
58 private static final Pattern METADATA_PATTERN = Pattern.compile( "maven-metadata-\\S*.xml" );
60 private final Path referenceFile;
63 * Construct a ChecksummedFile object.
65 * @param referenceFile
67 public ChecksummedFile( final Path referenceFile )
69 this.referenceFile = referenceFile;
73 * Calculate the checksum based on a given checksum.
75 * @param checksumAlgorithm the algorithm to use.
76 * @return the checksum string for the file.
77 * @throws IOException if unable to calculate the checksum.
79 public String calculateChecksum( ChecksumAlgorithm checksumAlgorithm )
83 try (InputStream fis = Files.newInputStream( referenceFile ))
85 Checksum checksum = new Checksum( checksumAlgorithm );
86 checksum.update( fis );
87 return checksum.getChecksum();
92 * Creates a checksum file of the provided referenceFile.
94 * @param checksumAlgorithm the hash to use.
95 * @return the checksum File that was created.
96 * @throws IOException if there was a problem either reading the referenceFile, or writing the checksum file.
98 public Path createChecksum( ChecksumAlgorithm checksumAlgorithm )
101 Path checksumFile = referenceFile.resolveSibling( referenceFile.getFileName() + "." + checksumAlgorithm.getExt() );
102 Files.deleteIfExists( checksumFile );
103 String checksum = calculateChecksum( checksumAlgorithm );
104 Files.write( checksumFile, //
105 ( checksum + " " + referenceFile.getFileName().toString() ).getBytes(), //
106 StandardOpenOption.CREATE_NEW );
111 * Get the checksum file for the reference file and hash.
113 * @param checksumAlgorithm the hash that we are interested in.
114 * @return the checksum file to return
116 public Path getChecksumFile( ChecksumAlgorithm checksumAlgorithm )
118 return referenceFile.resolveSibling( referenceFile.getFileName() + "." + checksumAlgorithm.getExt() );
123 * Given a checksum file, check to see if the file it represents is valid according to the checksum.
126 * NOTE: Only supports single file checksums of type MD5 or SHA1.
129 * @param algorithm the algorithms to check for.
130 * @return true if the checksum is valid for the file it represents. or if the checksum file does not exist.
131 * @throws IOException if the reading of the checksumFile or the file it refers to fails.
133 public boolean isValidChecksum( ChecksumAlgorithm algorithm )
136 return isValidChecksums( new ChecksumAlgorithm[]{ algorithm } );
140 * Of any checksum files present, validate that the reference file conforms
141 * the to the checksum.
143 * @param algorithms the algorithms to check for.
144 * @return true if the checksums report that the the reference file is valid, false if invalid.
146 public boolean isValidChecksums( ChecksumAlgorithm algorithms[] )
149 try (InputStream fis = Files.newInputStream( referenceFile))
151 List<Checksum> checksums = new ArrayList<>( algorithms.length );
152 // Create checksum object for each algorithm.
153 for ( ChecksumAlgorithm checksumAlgorithm : algorithms )
155 Path checksumFile = getChecksumFile( checksumAlgorithm );
157 // Only add algorithm if checksum file exists.
158 if ( Files.exists(checksumFile) )
160 checksums.add( new Checksum( checksumAlgorithm ) );
165 if ( checksums.isEmpty() )
167 // No checksum objects, no checksum files, default to is invalid.
171 // Parse file once, for all checksums.
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 Path checksumFile = getChecksumFile( checksumAlgorithm );
192 String rawChecksum = FileUtils.readFileToString( checksumFile , FILE_ENCODING );
193 String expectedChecksum = parseChecksum( rawChecksum, checksumAlgorithm, referenceFile.getFileName().toString() );
195 if ( !StringUtils.equalsIgnoreCase( expectedChecksum, checksum.getChecksum() ) )
201 catch ( IOException e )
203 log.warn( "Unable to read / parse checksum: {}", e.getMessage() );
209 catch ( IOException e )
211 log.warn( "Unable to read / parse checksum: {}", e.getMessage() );
217 * Fix or create checksum files for the reference file.
219 * @param algorithms the hashes to check for.
220 * @return true if checksums were created successfully.
222 public boolean fixChecksums( ChecksumAlgorithm[] algorithms )
224 List<Checksum> checksums = new ArrayList<>( algorithms.length );
225 // Create checksum object for each algorithm.
226 for ( ChecksumAlgorithm checksumAlgorithm : algorithms )
228 checksums.add( new Checksum( checksumAlgorithm ) );
232 if ( checksums.isEmpty() )
234 // No checksum objects, no checksum files, default to is valid.
238 try (InputStream fis = Files.newInputStream( referenceFile ))
240 // Parse file once, for all checksums.
241 Checksum.update( checksums, fis );
243 catch ( IOException e )
245 log.warn( e.getMessage(), e );
249 boolean valid = true;
251 // check the hash files
252 for ( Checksum checksum : checksums )
254 ChecksumAlgorithm checksumAlgorithm = checksum.getAlgorithm();
257 Path checksumFile = getChecksumFile( checksumAlgorithm );
258 String actualChecksum = checksum.getChecksum();
260 if ( Files.exists(checksumFile) )
262 String rawChecksum = FileUtils.readFileToString( checksumFile, FILE_ENCODING);
263 String expectedChecksum = parseChecksum( rawChecksum, checksumAlgorithm, referenceFile.getFileName().toString() );
265 if ( !StringUtils.equalsIgnoreCase( expectedChecksum, actualChecksum ) )
267 // create checksum (again)
268 FileUtils.writeStringToFile( checksumFile, FILE_ENCODING, actualChecksum + " " + referenceFile.getFileName().toString());
273 FileUtils.writeStringToFile( checksumFile, FILE_ENCODING, actualChecksum + " " + referenceFile.getFileName().toString() );
276 catch ( IOException e )
278 log.warn( e.getMessage(), e );
287 private boolean isValidChecksumPattern( String filename, String path )
289 // check if it is a remote metadata file
291 Matcher m = METADATA_PATTERN.matcher( path );
294 return filename.endsWith( path ) || ( "-".equals( filename ) ) || filename.endsWith( "maven-metadata.xml" );
297 return filename.endsWith( path ) || ( "-".equals( filename ) );
301 * Parse a checksum string.
303 * Validate the expected path, and expected checksum algorithm, then return
304 * the trimmed checksum hex string.
307 * @param rawChecksumString
308 * @param expectedHash
309 * @param expectedPath
311 * @throws IOException
313 public String parseChecksum( String rawChecksumString, ChecksumAlgorithm expectedHash, String expectedPath )
316 String trimmedChecksum = rawChecksumString.replace( '\n', ' ' ).trim();
318 // Free-BSD / openssl
319 String regex = expectedHash.getType() + "\\s*\\(([^)]*)\\)\\s*=\\s*([a-fA-F0-9]+)";
320 Matcher m = Pattern.compile( regex ).matcher( trimmedChecksum );
323 String filename = m.group( 1 );
324 if ( !isValidChecksumPattern( filename, expectedPath ) )
326 throw new IOException(
327 "Supplied checksum file '" + filename + "' does not match expected file: '" + expectedPath + "'" );
329 trimmedChecksum = m.group( 2 );
334 m = Pattern.compile( "([a-fA-F0-9]+)\\s+\\*?(.+)" ).matcher( trimmedChecksum );
337 String filename = m.group( 2 );
338 if ( !isValidChecksumPattern( filename, expectedPath ) )
340 throw new IOException(
341 "Supplied checksum file '" + filename + "' does not match expected file: '" + expectedPath
344 trimmedChecksum = m.group( 1 );
347 return trimmedChecksum;