feat(multiplayer): verify checksum. Closes #1122.

This commit is contained in:
huanghongxun
2021-10-23 15:29:14 +08:00
parent d167c712a6
commit 8d559dba02
3 changed files with 59 additions and 18 deletions

View File

@@ -18,6 +18,11 @@
package org.jackhuang.hmcl.util.io;
import org.jackhuang.hmcl.download.ArtifactMalformedException;
import org.jackhuang.hmcl.util.DigestUtils;
import org.jackhuang.hmcl.util.Hex;
import java.io.IOException;
import java.nio.file.Path;
public class ChecksumMismatchException extends ArtifactMalformedException {
@@ -43,4 +48,11 @@ public class ChecksumMismatchException extends ArtifactMalformedException {
public String getActualChecksum() {
return actualChecksum;
}
public static void verifyChecksum(Path file, String algorithm, String expectedChecksum) throws IOException {
String checksum = Hex.encodeHex(DigestUtils.digest(algorithm, file));
if (!checksum.equalsIgnoreCase(expectedChecksum)) {
throw new ChecksumMismatchException(algorithm, expectedChecksum, checksum);
}
}
}