Class VerifiedDownload
A catalogue answer is metadata on the outside of a file: the version names the byte count and
the SHA-256 of the JAR it offers, and the download response repeats the hash in
X-Content-SHA256. This class accepts a downloaded file only when the byte count equals the
metadata's sizeBytes, the file's own SHA-256 equals the metadata's sha256, and
the response header, when present, names that same hash. Upper-case hex is accepted from either
source; the comparison itself is plain equality, because these are public values and a timing
attack on a public hash buys nothing.
Metadata without a usable hash or byte count is refused, never treated as "no check": a
catalogue that omits the hash cannot downgrade verification. A body longer than
sizeBytes is cut off while streaming instead of being read to the end, and a 3xx is never
followed, so the JAR always comes from the host the base URL names.
The temporary file never survives a failed verification. It is the caller's file, named inside the modules folder so that accepting it stays one atomic rename.
- Since:
- 6.3.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enumWhy a download was or was not accepted.(package private) static final classWhat one verified download did. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static voiddeleteQuietly(Path file) (package private) static VerifiedDownload.ResultDownloadsurlintotempFileand verifies it.(package private) static booleanisSha256Hex(String value) (package private) static Stringprivate static VerifiedDownload.KindverdictOf(CatalogueHttp.Download download, String expectedSha256, long expectedSize) private static VerifiedDownload.KindverdictOfCompleted(CatalogueHttp.Download download, String expectedSha256, long expectedSize)
-
Field Details
-
SHA256_HEX_LENGTH
private static final int SHA256_HEX_LENGTH- See Also:
-
-
Constructor Details
-
VerifiedDownload
private VerifiedDownload()
-
-
Method Details
-
fetch
static VerifiedDownload.Result fetch(String url, String expectedSha256, long expectedSize, Path tempFile) DownloadsurlintotempFileand verifies it.- Parameters:
url- the absolute download URL, ornullwhen the version offers noneexpectedSha256- the version'ssha256, as it was writtenexpectedSize- the version'ssizeBytestempFile- the file to write and, on success, to keep- Returns:
- the outcome; never
null
-
verdictOf
private static VerifiedDownload.Kind verdictOf(CatalogueHttp.Download download, String expectedSha256, long expectedSize) -
verdictOfCompleted
private static VerifiedDownload.Kind verdictOfCompleted(CatalogueHttp.Download download, String expectedSha256, long expectedSize) -
isSha256Hex
- Parameters:
value- a value as written- Returns:
- whether it is exactly 64 hexadecimal characters
-
shortHash
- Parameters:
hash- a hash as written- Returns:
- at most the first 12 characters, for a local diagnostic line
-
deleteQuietly
-