Skip to content

Commit

Permalink
Fix String/StringBuilder comparison.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Boerman committed Dec 25, 2024
1 parent aa3e58e commit bd0c8d8
Showing 1 changed file with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -174,28 +175,31 @@ 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) {
throw new IOException("Could not find SHA-1 MessageDigest.", e);
}
}

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).
Expand Down

0 comments on commit bd0c8d8

Please sign in to comment.