|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | +package com.github.sbt.digest.util; |
| 19 | + |
| 20 | +import java.io.BufferedReader; |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.FileReader; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.security.MessageDigest; |
| 27 | +import java.security.NoSuchAlgorithmException; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Locale; |
| 30 | +import java.util.Map; |
| 31 | +import org.apache.ivy.util.FileUtil; |
| 32 | + |
| 33 | +public final class ChecksumHelper { |
| 34 | + |
| 35 | + private static final int BUFFER_SIZE = 2048; |
| 36 | + private static final Map<String, String> algorithms = new HashMap<>(); |
| 37 | + static { |
| 38 | + algorithms.put("md5", "MD5"); |
| 39 | + algorithms.put("sha1", "SHA-1"); |
| 40 | + |
| 41 | + // higher versions of JRE support these algorithms |
| 42 | + // https://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html#MessageDigest |
| 43 | + // conditionally add them |
| 44 | + if (isAlgorithmSupportedInJRE("SHA-256")) { |
| 45 | + algorithms.put("SHA-256", "SHA-256"); |
| 46 | + } |
| 47 | + if (isAlgorithmSupportedInJRE("SHA-512")) { |
| 48 | + algorithms.put("SHA-512", "SHA-512"); |
| 49 | + } |
| 50 | + if (isAlgorithmSupportedInJRE("SHA-384")) { |
| 51 | + algorithms.put("SHA-384", "SHA-384"); |
| 52 | + } |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + private static boolean isAlgorithmSupportedInJRE(final String algorithm) { |
| 57 | + if (algorithm == null) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + try { |
| 61 | + MessageDigest.getInstance(algorithm); |
| 62 | + return true; |
| 63 | + } catch (NoSuchAlgorithmException e) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Checks the checksum of the given file against the given checksumFile, and throws an |
| 70 | + * IOException if the checksum is not compliant |
| 71 | + * |
| 72 | + * @param dest |
| 73 | + * the file to test |
| 74 | + * @param checksumFile |
| 75 | + * the file containing the expected checksum |
| 76 | + * @param algorithm |
| 77 | + * the checksum algorithm to use |
| 78 | + * @throws IOException |
| 79 | + * if an IO problem occur while reading files or if the checksum is not compliant |
| 80 | + */ |
| 81 | + public static void check(File dest, File checksumFile, String algorithm) throws IOException { |
| 82 | + String csFileContent = FileUtil |
| 83 | + .readEntirely(new BufferedReader(new FileReader(checksumFile))).trim() |
| 84 | + .toLowerCase(Locale.US); |
| 85 | + String expected; |
| 86 | + if (csFileContent.indexOf(' ') > -1 |
| 87 | + && (csFileContent.startsWith("md") || csFileContent.startsWith("sha"))) { |
| 88 | + int lastSpaceIndex = csFileContent.lastIndexOf(' '); |
| 89 | + expected = csFileContent.substring(lastSpaceIndex + 1); |
| 90 | + } else { |
| 91 | + int spaceIndex = csFileContent.indexOf(' '); |
| 92 | + if (spaceIndex != -1) { |
| 93 | + expected = csFileContent.substring(0, spaceIndex); |
| 94 | + |
| 95 | + // IVY-1155: support some strange formats like this one: |
| 96 | + // https://repo1.maven.org/maven2/org/apache/pdfbox/fontbox/0.8.0-incubator/fontbox-0.8.0-incubator.jar.md5 |
| 97 | + if (expected.endsWith(":")) { |
| 98 | + StringBuilder result = new StringBuilder(); |
| 99 | + for (char ch : csFileContent.substring(spaceIndex + 1).toCharArray()) { |
| 100 | + if (!Character.isWhitespace(ch)) { |
| 101 | + result.append(ch); |
| 102 | + } |
| 103 | + } |
| 104 | + expected = result.toString(); |
| 105 | + } |
| 106 | + } else { |
| 107 | + expected = csFileContent; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + String computed = computeAsString(dest, algorithm).trim().toLowerCase(Locale.US); |
| 112 | + if (!expected.equals(computed)) { |
| 113 | + throw new IOException("invalid " + algorithm + ": expected=" + expected + " computed=" |
| 114 | + + computed); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public static String computeAsString(File f, String algorithm) throws IOException { |
| 119 | + return byteArrayToHexString(compute(f, algorithm)); |
| 120 | + } |
| 121 | + |
| 122 | + private static byte[] compute(File f, String algorithm) throws IOException { |
| 123 | + |
| 124 | + try (InputStream is = new FileInputStream(f)) { |
| 125 | + MessageDigest md = getMessageDigest(algorithm); |
| 126 | + md.reset(); |
| 127 | + |
| 128 | + byte[] buf = new byte[BUFFER_SIZE]; |
| 129 | + int len = 0; |
| 130 | + while ((len = is.read(buf)) != -1) { |
| 131 | + md.update(buf, 0, len); |
| 132 | + } |
| 133 | + return md.digest(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public static boolean isKnownAlgorithm(String algorithm) { |
| 138 | + return algorithms.containsKey(algorithm); |
| 139 | + } |
| 140 | + |
| 141 | + private static MessageDigest getMessageDigest(String algorithm) { |
| 142 | + String mdAlgorithm = algorithms.get(algorithm); |
| 143 | + if (mdAlgorithm == null) { |
| 144 | + throw new IllegalArgumentException("unknown algorithm " + algorithm); |
| 145 | + } |
| 146 | + try { |
| 147 | + return MessageDigest.getInstance(mdAlgorithm); |
| 148 | + } catch (NoSuchAlgorithmException e) { |
| 149 | + throw new IllegalArgumentException("unknown algorithm " + algorithm); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // byte to hex string converter |
| 154 | + private static final char[] CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', |
| 155 | + 'b', 'c', 'd', 'e', 'f'}; |
| 156 | + |
| 157 | + /** |
| 158 | + * Convert a byte[] array to readable string format. This makes the "hex" readable! |
| 159 | + * |
| 160 | + * @return result String buffer in String format |
| 161 | + * @param in |
| 162 | + * byte[] buffer to convert to string format |
| 163 | + */ |
| 164 | + public static String byteArrayToHexString(byte[] in) { |
| 165 | + byte ch = 0x00; |
| 166 | + |
| 167 | + if (in == null || in.length <= 0) { |
| 168 | + return null; |
| 169 | + } |
| 170 | + |
| 171 | + StringBuilder out = new StringBuilder(in.length * 2); |
| 172 | + |
| 173 | + // CheckStyle:MagicNumber OFF |
| 174 | + for (byte bt : in) { |
| 175 | + ch = (byte) (bt & 0xF0); // Strip off high nibble |
| 176 | + ch = (byte) (ch >>> 4); // shift the bits down |
| 177 | + ch = (byte) (ch & 0x0F); // must do this is high order bit is on! |
| 178 | + |
| 179 | + out.append(CHARS[ch]); // convert the nibble to a String Character |
| 180 | + ch = (byte) (bt & 0x0F); // Strip off low nibble |
| 181 | + out.append(CHARS[ch]); // convert the nibble to a String Character |
| 182 | + } |
| 183 | + // CheckStyle:MagicNumber ON |
| 184 | + |
| 185 | + return out.toString(); |
| 186 | + } |
| 187 | + |
| 188 | + private ChecksumHelper() { |
| 189 | + } |
| 190 | +} |
0 commit comments