chore: hash MD5 with the shared Digests helper (#7570)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-04 22:52:59 -04:00
1 parent 35b496124e
commit 189521e5cc
6 files changed
+22 -62

No files matched your search

@@ -5,8 +5,6 @@ import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
@@ -277,11 +275,6 @@ public final class SshPublicKeys {
/** MD5 of the given bytes as colon-delimited lowercase hex. */
public static String md5ColonHex(byte[] bytes) {
try {
return HexFormat.of().withDelimiter(":")
.formatHex(MessageDigest.getInstance("MD5").digest(bytes));
} catch (NoSuchAlgorithmException e) {
throw new SshPublicKeyException("MD5 is unavailable", e);
}
return HexFormat.of().withDelimiter(":").formatHex(Digests.md5(bytes));
}
}
@@ -54,8 +54,6 @@ import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
@@ -4862,17 +4860,12 @@ public class DynamoDbService {
if (scalar == null) {
return 0;
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(scalar.getBytes(StandardCharsets.UTF_8));
long val = ((long) (digest[0] & 0xFF) << 24)
| ((long) (digest[1] & 0xFF) << 16)
| ((long) (digest[2] & 0xFF) << 8)
| ((long) (digest[3] & 0xFF));
return (int) (val % totalSegments);
} catch (NoSuchAlgorithmException e) {
return Math.floorMod(scalar.hashCode(), totalSegments);
}
byte[] digest = Digests.md5(scalar.getBytes(StandardCharsets.UTF_8));
long val = ((long) (digest[0] & 0xFF) << 24)
| ((long) (digest[1] & 0xFF) << 16)
| ((long) (digest[2] & 0xFF) << 8)
| ((long) (digest[3] & 0xFF));
return (int) (val % totalSegments);
}
private String extractScalarValue(JsonNode attrValue) {
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Base64Url;
import io.github.hectorvent.floci.core.common.Digests;
import io.github.hectorvent.floci.core.common.EpochSeconds;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
@@ -55,8 +56,6 @@ import org.jboss.logging.Logger;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
@@ -3129,12 +3128,7 @@ public class GlueService {
* derives it from the document so the same policy always yields the same hash.
*/
private static String policyHash(String policyInJson) {
try {
byte[] digest = MessageDigest.getInstance("MD5").digest(policyInJson.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(digest);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("MD5 is a required JDK algorithm", e);
}
return Base64.getEncoder().encodeToString(Digests.md5(policyInJson.getBytes(StandardCharsets.UTF_8)));
}
public void tagResource(String arn, Map<String, String> tags, String region) {
@@ -869,19 +869,15 @@ public class PostgresProtocolHandler {
}
private static String computeMd5Password(String password, String username, byte[] salt) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(password.getBytes(StandardCharsets.UTF_8));
md5.update(username.getBytes(StandardCharsets.UTF_8));
String hex1 = bytesToHex(md5.digest());
MessageDigest md5 = Digests.md5Digest();
md5.update(password.getBytes(StandardCharsets.UTF_8));
md5.update(username.getBytes(StandardCharsets.UTF_8));
String hex1 = bytesToHex(md5.digest());
md5.reset();
md5.update(hex1.getBytes(StandardCharsets.UTF_8));
md5.update(salt);
return "md5" + bytesToHex(md5.digest());
} catch (Exception e) {
throw new RuntimeException("MD5 computation failed", e);
}
md5.reset();
md5.update(hex1.getBytes(StandardCharsets.UTF_8));
md5.update(salt);
return "md5" + bytesToHex(md5.digest());
}
// ── Post-auth buffering ───────────────────────────────────────────────────
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.AwsCanonicalUserId;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsNamespaces;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.Digests;
import io.github.hectorvent.floci.core.common.IamEnforcementFilter;
import io.github.hectorvent.floci.core.common.MultipartFormParser;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -64,8 +65,6 @@ import java.io.UncheckedIOException;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
@@ -3617,13 +3616,7 @@ public class S3Controller {
private void validateChecksumHeaders(HttpHeaders httpHeaders, UriInfo uriInfo, byte[] data) {
UploadChecksums checksums = uploadChecksums(httpHeaders, uriInfo);
checksums.requireWellFormedContentMd5();
byte[] md5;
try {
md5 = MessageDigest.getInstance("MD5").digest(data);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("MD5 algorithm is not available", e);
}
checksums.verify(md5, claimedAlgorithm -> claimedAlgorithm.compute(data));
checksums.verify(Digests.md5(data), claimedAlgorithm -> claimedAlgorithm.compute(data));
}
private UploadChecksums uploadChecksums(HttpHeaders httpHeaders, UriInfo uriInfo) {
@@ -65,7 +65,6 @@ import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
@@ -3001,7 +3000,7 @@ public class S3Service implements Resettable, ResourceProvider {
/** Reads {@code file} once, feeding {@code checksum} if given; returns its MD5 ETag when {@code md5}. */
private static String hashFile(Path file, boolean md5, S3Checksum.Calculator checksum) {
try {
MessageDigest digest = md5 ? MessageDigest.getInstance("MD5") : null;
MessageDigest digest = md5 ? Digests.md5Digest() : null;
byte[] buffer = new byte[1 << 20];
try (InputStream in = Files.newInputStream(file)) {
for (int read = in.read(buffer); read >= 0; read = in.read(buffer)) {
@@ -3016,8 +3015,6 @@ public class S3Service implements Resettable, ResourceProvider {
return digest == null ? null : "\"" + bytesToHex(digest.digest()) + "\"";
} catch (IOException e) {
throw new UncheckedIOException("Failed to read the copy source", e);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("MD5 algorithm not available", e);
}
}
@@ -5219,7 +5216,7 @@ public class S3Service implements Resettable, ResourceProvider {
// Concatenate parts in order
try {
MessageDigest md = MessageDigest.getInstance("MD5");
MessageDigest md = Digests.md5Digest();
for (Part part : parts) {
// A part ETag is the MD5 of that part, so the composite hashes it without rehashing the data
String partETag = stripSurroundingQuotes(part.getETag());
@@ -5300,8 +5297,6 @@ public class S3Service implements Resettable, ResourceProvider {
return object;
} catch (IOException e) {
throw new UncheckedIOException("Failed to read multipart parts", e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 algorithm not available", e);
}
});
fireNotifications(bucket, key, "ObjectCreated:CompleteMultipartUpload", completed);
@@ -7048,16 +7043,12 @@ public class S3Service implements Resettable, ResourceProvider {
throw new AwsException("InvalidArgument",
"The x-amz-server-side-encryption-customer-key must be a 256-bit key.", 400);
}
byte[] md5 = MessageDigest.getInstance("MD5").digest(decodedKey);
return Base64.getEncoder().encodeToString(md5);
return Base64.getEncoder().encodeToString(Digests.md5(decodedKey));
}
catch (IllegalArgumentException e) {
throw new AwsException("InvalidArgument",
"The x-amz-server-side-encryption-customer-key value is not valid base64.", 400);
}
catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("MD5 is not available", e);
}
}
record SseCustomerKey(String algorithm, String keyMd5) {}