-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cf: verify hash of downloaded mod files and repair (#435)
- Loading branch information
Showing
6 changed files
with
162 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/me/itzg/helpers/curseforge/FileHashInvalidException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package me.itzg.helpers.curseforge; | ||
|
||
public class FileHashInvalidException extends RuntimeException { | ||
|
||
public FileHashInvalidException(String message) { | ||
super(message); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/me/itzg/helpers/curseforge/FileHashVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package me.itzg.helpers.curseforge; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.EnumMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.extern.slf4j.Slf4j; | ||
import me.itzg.helpers.curseforge.model.FileHash; | ||
import me.itzg.helpers.curseforge.model.HashAlgo; | ||
import me.itzg.helpers.files.ChecksumAlgo; | ||
import me.itzg.helpers.files.Checksums; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Schedulers; | ||
|
||
@Slf4j | ||
public class FileHashVerifier { | ||
|
||
private final static Map<HashAlgo, ChecksumAlgo> algos = new EnumMap<>(HashAlgo.class); | ||
|
||
static { | ||
algos.put(HashAlgo.Md5, ChecksumAlgo.MD5); | ||
algos.put(HashAlgo.Sha1, ChecksumAlgo.SHA1); | ||
} | ||
|
||
public static Mono<Path> verify(Path file, List<FileHash> hashes) { | ||
for (final FileHash hash : hashes) { | ||
final ChecksumAlgo checksumAlgo = algos.get(hash.getAlgo()); | ||
if (checksumAlgo != null) { | ||
return Mono.fromCallable(() -> { | ||
log.debug("Verifying hash of {}", file); | ||
|
||
if (!Checksums.valid(file, checksumAlgo, hash.getValue())) { | ||
Files.delete(file); | ||
throw new FileHashInvalidException("Incorrect checksum: " + file); | ||
} | ||
else { | ||
return file; | ||
} | ||
}) | ||
.subscribeOn(Schedulers.boundedElastic()); | ||
} | ||
} | ||
|
||
return Mono.error(new IllegalArgumentException("Unable to find compatible checksum algorithm")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package me.itzg.helpers.files; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum ChecksumAlgo { | ||
MD5("md5", "MD5"), | ||
SHA1("sha1", "SHA-1"), | ||
SHA256("sha256", "SHA-256"),; | ||
|
||
private final String prefix; | ||
|
||
private final String jdkAlgo; | ||
|
||
ChecksumAlgo(String prefix, String jdkAlgo) { | ||
this.prefix = prefix; | ||
this.jdkAlgo = jdkAlgo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/test/java/me/itzg/helpers/curseforge/FileHashVerifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package me.itzg.helpers.curseforge; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import me.itzg.helpers.curseforge.model.FileHash; | ||
import me.itzg.helpers.curseforge.model.HashAlgo; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
@SuppressWarnings("CodeBlock2Expr") | ||
class FileHashVerifierTest { | ||
|
||
@Test | ||
void validMd5(@TempDir Path tempDir) throws Exception { | ||
final Path file = Files.write(tempDir.resolve("hello.txt"), "Hello World".getBytes(StandardCharsets.UTF_8)); | ||
|
||
assertThatCode(() -> { | ||
FileHashVerifier.verify(file, singletonList( | ||
new FileHash().setAlgo(HashAlgo.Md5).setValue("B10A8DB164E0754105B7A99BE72E3FE5") | ||
)) | ||
.block(); | ||
}) | ||
.doesNotThrowAnyException(); | ||
} | ||
|
||
@Test | ||
void validSha1(@TempDir Path tempDir) throws IOException { | ||
final Path file = Files.write(tempDir.resolve("hello.txt"), "Hello World".getBytes(StandardCharsets.UTF_8)); | ||
|
||
assertThatCode(() -> { | ||
FileHashVerifier.verify(file, singletonList( | ||
new FileHash().setAlgo(HashAlgo.Sha1).setValue("0A4D55A8D778E5022FAB701977C5D840BBC486D0") | ||
)) | ||
.block(); | ||
}) | ||
.doesNotThrowAnyException(); | ||
|
||
} | ||
|
||
@Test | ||
void handlesInvalid(@TempDir Path tempDir) throws IOException { | ||
final Path file = Files.write(tempDir.resolve("hello.txt"), "Hello World".getBytes(StandardCharsets.UTF_8)); | ||
|
||
assertThatThrownBy(() -> { | ||
FileHashVerifier.verify(file, singletonList( | ||
new FileHash().setAlgo(HashAlgo.Md5).setValue("BAD") | ||
)) | ||
.block(); | ||
}) | ||
.isInstanceOf(FileHashInvalidException.class); | ||
} | ||
} |