package dev.lexian.remotepacksync; import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; import java.util.HexFormat; import java.util.regex.Matcher; import java.util.regex.Pattern; /** Downloads and validates a SHA-1 checksum without downloading the resource pack itself. */ public final class RemoteSha1Fetcher { private static final Pattern SHA1_PATTERN = Pattern.compile("(?i)(? response = client.send(request, HttpResponse.BodyHandlers.ofByteArray()); if (response.statusCode() < 200 || response.statusCode() >= 300) { throw new IOException("SHA-1 endpoint returned HTTP " + response.statusCode()); } if (response.body().length > MAX_RESPONSE_BYTES) { throw new IOException("SHA-1 response exceeds " + MAX_RESPONSE_BYTES + " bytes"); } String responseText = new String(response.body(), java.nio.charset.StandardCharsets.UTF_8); Matcher matcher = SHA1_PATTERN.matcher(responseText); if (!matcher.find()) { throw new IOException("SHA-1 response does not contain a 40-character hexadecimal SHA-1"); } return HexFormat.of().parseHex(matcher.group()); } }