Check if forge libraries' checksums are valid

This commit is contained in:
huangyuhui
2018-08-18 22:25:13 +08:00
parent ba2e419894
commit 82916a04ea

View File

@@ -74,10 +74,10 @@ public final class LibraryDownloadTask extends Task {
throw new LibraryDownloadException(library, t); throw new LibraryDownloadException(library, t);
} }
} else { } else {
if (xz) { if (xz) unpackLibrary(jar, FileUtils.readBytes(xzFile));
unpackLibrary(jar, FileUtils.readBytes(xzFile)); if (!checksumValid(jar, library.getChecksums())) {
if (!checksumValid(jar, library.getChecksums())) jar.delete();
throw new IOException("Checksum failed for " + library); throw new IOException("Checksum failed for " + library);
} }
} }
} }
@@ -121,14 +121,15 @@ public final class LibraryDownloadTask extends Task {
private static boolean checksumValid(File libPath, List<String> checksums) { private static boolean checksumValid(File libPath, List<String> checksums) {
try { try {
if ((checksums == null) || (checksums.isEmpty())) { if (checksums == null || checksums.isEmpty()) {
return true; return true;
} }
byte[] fileData = FileUtils.readBytes(libPath); byte[] fileData = FileUtils.readBytes(libPath);
boolean valid = checksums.contains(encodeHex(digest("SHA-1", fileData))); boolean valid = checksums.contains(encodeHex(digest("SHA-1", fileData)));
if ((!valid) && (libPath.getName().endsWith(".jar"))) { if (!valid && libPath.getName().endsWith(".jar")) {
valid = validateJar(fileData, checksums);
} }
return validateJar(fileData, checksums); return valid;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -152,8 +153,8 @@ public final class LibraryDownloadTask extends Task {
} }
jar.close(); jar.close();
if (hashes != null) { if (hashes != null) {
boolean passed = !checksums.contains(files.get("checksums.sha1")); boolean failed = !checksums.contains(files.get("checksums.sha1"));
if (passed) { if (!failed) {
for (String hash : hashes) { for (String hash : hashes) {
if ((!hash.trim().equals("")) && (hash.contains(" "))) { if ((!hash.trim().equals("")) && (hash.contains(" "))) {
String[] e = hash.split(" "); String[] e = hash.split(" ");
@@ -162,17 +163,17 @@ public final class LibraryDownloadTask extends Task {
String checksum = files.get(target); String checksum = files.get(target);
if ((!files.containsKey(target)) || (checksum == null)) { if ((!files.containsKey(target)) || (checksum == null)) {
Logging.LOG.warning(" " + target + " : missing"); Logging.LOG.warning(" " + target + " : missing");
passed = false; failed = true;
break; break;
} else if (!checksum.equals(validChecksum)) { } else if (!checksum.equals(validChecksum)) {
Logging.LOG.warning(" " + target + " : failed (" + checksum + ", " + validChecksum + ")"); Logging.LOG.warning(" " + target + " : failed (" + checksum + ", " + validChecksum + ")");
passed = false; failed = true;
break; break;
} }
} }
} }
} }
return passed; return !failed;
} }
return false; return false;
} }