001package org.eclipse.aether.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.BufferedReader; 023import java.io.ByteArrayInputStream; 024import java.io.File; 025import java.io.FileInputStream; 026import java.io.IOException; 027import java.io.InputStream; 028import java.io.InputStreamReader; 029import java.nio.charset.StandardCharsets; 030import java.security.MessageDigest; 031import java.security.NoSuchAlgorithmException; 032import java.util.Collection; 033import java.util.LinkedHashMap; 034import java.util.Map; 035 036/** 037 * A utility class to assist in the verification and generation of checksums. 038 */ 039public final class ChecksumUtils 040{ 041 042 private ChecksumUtils() 043 { 044 // hide constructor 045 } 046 047 /** 048 * Extracts the checksum from the specified file. 049 * 050 * @param checksumFile The path to the checksum file, must not be {@code null}. 051 * @return The checksum stored in the file, never {@code null}. 052 * @throws IOException If the checksum does not exist or could not be read for other reasons. 053 */ 054 public static String read( File checksumFile ) 055 throws IOException 056 { 057 String checksum = ""; 058 try ( BufferedReader br = new BufferedReader( new InputStreamReader( 059 new FileInputStream( checksumFile ), StandardCharsets.UTF_8 ), 512 ) ) 060 { 061 while ( true ) 062 { 063 String line = br.readLine(); 064 if ( line == null ) 065 { 066 break; 067 } 068 line = line.trim(); 069 if ( line.length() > 0 ) 070 { 071 checksum = line; 072 break; 073 } 074 } 075 } 076 077 if ( checksum.matches( ".+= [0-9A-Fa-f]+" ) ) 078 { 079 int lastSpacePos = checksum.lastIndexOf( ' ' ); 080 checksum = checksum.substring( lastSpacePos + 1 ); 081 } 082 else 083 { 084 int spacePos = checksum.indexOf( ' ' ); 085 086 if ( spacePos != -1 ) 087 { 088 checksum = checksum.substring( 0, spacePos ); 089 } 090 } 091 092 return checksum; 093 } 094 095 /** 096 * Calculates checksums for the specified file. 097 * 098 * @param dataFile The file for which to calculate checksums, must not be {@code null}. 099 * @param algos The names of checksum algorithms (cf. {@link MessageDigest#getInstance(String)} to use, must not be 100 * {@code null}. 101 * @return The calculated checksums, indexed by algorithm name, or the exception that occurred while trying to 102 * calculate it, never {@code null}. 103 * @throws IOException If the data file could not be read. 104 */ 105 public static Map<String, Object> calc( File dataFile, Collection<String> algos ) 106 throws IOException 107 { 108 return calc( new FileInputStream( dataFile ), algos ); 109 } 110 111 112 public static Map<String, Object> calc( byte[] dataBytes, Collection<String> algos ) 113 throws IOException 114 { 115 return calc( new ByteArrayInputStream( dataBytes ), algos ); 116 } 117 118 119 private static Map<String, Object> calc( InputStream data, Collection<String> algos ) 120 throws IOException 121 { 122 Map<String, Object> results = new LinkedHashMap<>(); 123 124 Map<String, MessageDigest> digests = new LinkedHashMap<>(); 125 for ( String algo : algos ) 126 { 127 try 128 { 129 digests.put( algo, MessageDigest.getInstance( algo ) ); 130 } 131 catch ( NoSuchAlgorithmException e ) 132 { 133 results.put( algo, e ); 134 } 135 } 136 137 try ( InputStream in = data ) 138 { 139 for ( byte[] buffer = new byte[ 32 * 1024 ];; ) 140 { 141 int read = in.read( buffer ); 142 if ( read < 0 ) 143 { 144 break; 145 } 146 for ( MessageDigest digest : digests.values() ) 147 { 148 digest.update( buffer, 0, read ); 149 } 150 } 151 } 152 153 for ( Map.Entry<String, MessageDigest> entry : digests.entrySet() ) 154 { 155 byte[] bytes = entry.getValue().digest(); 156 157 results.put( entry.getKey(), toHexString( bytes ) ); 158 } 159 160 return results; 161 } 162 163 164 /** 165 * Creates a hexadecimal representation of the specified bytes. Each byte is converted into a two-digit hex number 166 * and appended to the result with no separator between consecutive bytes. 167 * 168 * @param bytes The bytes to represent in hex notation, may be be {@code null}. 169 * @return The hexadecimal representation of the input or {@code null} if the input was {@code null}. 170 */ 171 @SuppressWarnings( "checkstyle:magicnumber" ) 172 public static String toHexString( byte[] bytes ) 173 { 174 if ( bytes == null ) 175 { 176 return null; 177 } 178 179 StringBuilder buffer = new StringBuilder( bytes.length * 2 ); 180 181 for ( byte aByte : bytes ) 182 { 183 int b = aByte & 0xFF; 184 if ( b < 0x10 ) 185 { 186 buffer.append( '0' ); 187 } 188 buffer.append( Integer.toHexString( b ) ); 189 } 190 191 return buffer.toString(); 192 } 193 194}