diff --git a/ScalaLoader-Common/src/main/java/xyz/janboerman/scalaloader/util/ScalaLoaderUtils.java b/ScalaLoader-Common/src/main/java/xyz/janboerman/scalaloader/util/ScalaLoaderUtils.java index cfd2bd4..c948281 100644 --- a/ScalaLoader-Common/src/main/java/xyz/janboerman/scalaloader/util/ScalaLoaderUtils.java +++ b/ScalaLoader-Common/src/main/java/xyz/janboerman/scalaloader/util/ScalaLoaderUtils.java @@ -15,6 +15,7 @@ import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Constructor; @@ -174,21 +175,7 @@ private static void checkSha1Hash(File outputFile, String sha1hash) throws IOExc if (sha1hash == null || sha1hash.isEmpty()) return; try { - MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); - try (FileInputStream fis = new FileInputStream(outputFile)) { - byte[] buffer = new byte[1024]; - int bytesRead; - while ((bytesRead = fis.read(buffer)) != -1) { - sha1.update(buffer, 0, bytesRead); - } - } - - StringBuilder hashValue = new StringBuilder(); - for (byte b : sha1.digest()) { - hashValue.append(String.format("%02x", b)); - } - - if (!sha1hash.equals(hashValue)) { + if (!sha1hash.equals(getSha1Hash(outputFile))) { throw new IOException("Unexpected hash for " + outputFile.getName() + ", expected: " + sha1hash + ", actual: " + hashValue); } } catch (NoSuchAlgorithmException e) { @@ -196,6 +183,23 @@ private static void checkSha1Hash(File outputFile, String sha1hash) throws IOExc } } + private static String getSha1Hash(File file) throws NoSuchAlgorithmException, IOException { + MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); + try (FileInputStream fis = new FileInputStream(file)) { + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = fis.read(buffer)) != -1) { + sha1.update(buffer, 0, bytesRead); + } + } + + StringBuilder hashValue = new StringBuilder(); + for (byte b : sha1.digest()) { + hashValue.append(String.format("%02x", b)); + } + return hashValue.toString(); + } + /** * Get a (fresh or cached) {@link ScalaLibraryClassLoader} that loads standard library classes from a specific Scala version. * The classloader can either load classes from over the network directly, or use downloaded library archives (jar files).