feat(multiplayer): verify checksum. Closes #1122.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user