Skip to content

Commit

Permalink
Implement dummy variable url hash type
Browse files Browse the repository at this point in the history
  • Loading branch information
booky10 committed May 12, 2024
1 parent 0870af9 commit 79885d1
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions common/src/main/java/dev/booky/stackdeobf/http/VerifiableUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
import java.net.http.HttpRequest;
Expand Down Expand Up @@ -41,6 +42,11 @@ public static CompletableFuture<VerifiableUrl> resolve(URI url, HashType hashTyp
}

public static CompletableFuture<VerifiableUrl> resolve(URI url, HashType hashType, Executor executor, int depth) {
if (hashType == HashType.DUMMY) {
VerifiableUrl dummyUrl = new VerifiableUrl(url, HashType.DUMMY, HashResult.emptyResult());
return CompletableFuture.completedFuture(dummyUrl);
}

LOGGER.info("Downloading {} hash for {}...", hashType, url);
URI hashUrl = URI.create(url + hashType.getExtension());
return HttpUtil.getAsync(hashUrl, executor, depth)
Expand Down Expand Up @@ -82,6 +88,10 @@ public CompletableFuture<HttpResponseContainer> get(Executor executor) {

public CompletableFuture<HttpResponseContainer> get(Executor executor, int depth) {
return HttpUtil.getAsync(this.uri, executor, depth).thenCompose(resp -> {
if (this.hashType == HashType.DUMMY) {
return CompletableFuture.completedFuture(resp);
}

HashResult hashResult = this.hashType.hash(resp.getBody());
LOGGER.info("Verifying {} hash {} for {}...",
this.hashType, hashResult, this.uri);
Expand All @@ -104,22 +114,33 @@ public enum HashType {
MD5(".md5", "MD5"),
SHA1(".sha1", "SHA-1"),
SHA256(".sha256", "SHA-256"),
SHA512(".sha512", "SHA-512");
SHA512(".sha512", "SHA-512"),
DUMMY(null, null);

private final String extension;
private final MessageDigest digester;
private final @Nullable String extension;
private final @Nullable MessageDigest digester;

HashType(String extension, String algoName) {
HashType(@Nullable String extension, @Nullable String algoName) {
if ((extension == null) != (algoName == null)) {
throw new IllegalArgumentException("Can't create half-valid hash type");
}
this.extension = extension;

try {
this.digester = MessageDigest.getInstance(algoName);
} catch (NoSuchAlgorithmException exception) {
throw new IllegalStateException("Illegal algorithm provided: " + algoName, exception);
if (algoName != null) {
try {
this.digester = MessageDigest.getInstance(algoName);
} catch (NoSuchAlgorithmException exception) {
throw new IllegalStateException("Illegal algorithm provided: " + algoName, exception);
}
} else {
this.digester = null;
}
}

public HashResult hash(byte[] bytes) {
if (this.digester == null) {
throw new UnsupportedOperationException();
}
byte[] resultBytes;
synchronized (this.digester) {
resultBytes = this.digester.digest(bytes);
Expand All @@ -128,6 +149,9 @@ public HashResult hash(byte[] bytes) {
}

public String getExtension() {
if (this.extension == null) {
throw new UnsupportedOperationException();
}
return this.extension;
}
}
Expand All @@ -140,6 +164,10 @@ public HashResult(String string) {
this(decode(string));
}

public static HashResult emptyResult() {
return new HashResult(new byte[0]);
}

private static byte[] decode(String string) {
if (string.length() % 2 != 0) {
throw new IllegalArgumentException("Illegal hash string: " + string);
Expand All @@ -154,6 +182,10 @@ private static byte[] decode(String string) {
return bytes;
}

public boolean isEmpty() {
return this.bytes.length == 0;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
Expand Down

0 comments on commit 79885d1

Please sign in to comment.