mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
chore: format bytes as hex with HexFormat (#7592)
This commit is contained in:
1 parent
903430576a
commit
85f5da654c
11 files changed
+24
-76
No files matched your search
+2
-5
@@ -16,6 +16,7 @@ import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -368,10 +369,6 @@ public final class SigV4RequestValidator {
|
||||
}
|
||||
|
||||
public static String hexEncode(byte[] bytes) {
|
||||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
return HexFormat.of().formatHex(bytes);
|
||||
}
|
||||
}
|
||||
+2
-5
@@ -17,6 +17,7 @@ import org.jboss.logging.Logger;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -235,11 +236,7 @@ public class BedrockAgentCoreEventService {
|
||||
long millis = Math.round(eventTimestampSeconds * 1000d);
|
||||
byte[] suffix = new byte[4];
|
||||
RANDOM.nextBytes(suffix);
|
||||
StringBuilder hex = new StringBuilder();
|
||||
for (byte b : suffix) {
|
||||
hex.append(String.format("%02x", b));
|
||||
}
|
||||
return String.format("%019d", millis) + "#" + hex;
|
||||
return String.format("%019d", millis) + "#" + HexFormat.of().formatHex(suffix);
|
||||
}
|
||||
|
||||
private static MemoryEvent withoutPayload(MemoryEvent source) {
|
||||
|
||||
@@ -133,6 +133,7 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -2226,12 +2227,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
|
||||
/** Synthesizes a locally-administered unicast MAC (AWS never discloses how it derives its own). */
|
||||
private String randomMac() {
|
||||
Random rand = new Random();
|
||||
StringBuilder sb = new StringBuilder("02");
|
||||
for (int i = 0; i < 5; i++) {
|
||||
sb.append(':').append(String.format("%02x", rand.nextInt(256)));
|
||||
}
|
||||
return sb.toString();
|
||||
byte[] bytes = new byte[5];
|
||||
new Random().nextBytes(bytes);
|
||||
return "02:" + HexFormat.ofDelimiter(":").formatHex(bytes);
|
||||
}
|
||||
|
||||
// ─── Transit Gateways ──────────────────────────────────────────────────────
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -97,7 +98,7 @@ class EksTokenValidator {
|
||||
+ parameters.get("X-Amz-Date") + "\n"
|
||||
+ scope.value() + "\n"
|
||||
+ Digests.sha256Hex(canonicalRequest);
|
||||
String expectedSignature = hexEncode(hmacSha256(
|
||||
String expectedSignature = HexFormat.of().formatHex(hmacSha256(
|
||||
deriveSigningKey(secretKey, scope.date(), scope.region(), scope.service()), stringToSign));
|
||||
boolean valid = MessageDigest.isEqual(
|
||||
expectedSignature.getBytes(StandardCharsets.UTF_8),
|
||||
@@ -232,14 +233,6 @@ class EksTokenValidator {
|
||||
return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String hexEncode(byte[] bytes) {
|
||||
StringBuilder encoded = new StringBuilder(bytes.length * 2);
|
||||
for (byte value : bytes) {
|
||||
encoded.append(String.format("%02x", value));
|
||||
}
|
||||
return encoded.toString();
|
||||
}
|
||||
|
||||
private record CredentialScope(String accessKeyId, String date, String region, String service) {
|
||||
private String value() {
|
||||
return date + "/" + region + "/" + service + "/aws4_request";
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.security.SecureRandom;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -599,11 +600,7 @@ public class ElbClassicService implements ServerCertificateReferenceProvider {
|
||||
private static String randomHex() {
|
||||
byte[] bytes = new byte[5];
|
||||
RANDOM.nextBytes(bytes);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
return HexFormat.of().formatHex(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-9
@@ -16,6 +16,7 @@ import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -296,7 +297,7 @@ final class EksTokenMinter {
|
||||
String stringToSign = "AWS4-HMAC-SHA256\n" + amzDate + "\n" + credentialScope + "\n"
|
||||
+ Digests.sha256Hex(canonicalRequest);
|
||||
byte[] signingKey = deriveSigningKey(credentials.secretAccessKey(), dateStamp, region, "sts");
|
||||
String signature = hexEncode(hmacSha256(signingKey, stringToSign));
|
||||
String signature = HexFormat.of().formatHex(hmacSha256(signingKey, stringToSign));
|
||||
|
||||
String url = "https://" + host + "/?" + canonicalQueryString + "&X-Amz-Signature=" + signature;
|
||||
return TOKEN_PREFIX + Base64Url.encode(url);
|
||||
@@ -459,12 +460,4 @@ final class EksTokenMinter {
|
||||
throw new IllegalStateException("Could not compute HMAC-SHA256: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String hexEncode(byte[] bytes) {
|
||||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+2
-6
@@ -8,6 +8,7 @@ import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Arrays;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
@@ -410,12 +411,7 @@ final class PaymentCryptographyCrypto {
|
||||
}
|
||||
|
||||
static String toHex(byte[] bytes) {
|
||||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
||||
for (byte b : bytes) {
|
||||
sb.append(Character.forDigit((b >> 4) & 0x0f, 16));
|
||||
sb.append(Character.forDigit(b & 0x0f, 16));
|
||||
}
|
||||
return sb.toString();
|
||||
return HexFormat.of().formatHex(bytes);
|
||||
}
|
||||
|
||||
static byte[] fromHex(String hex) {
|
||||
|
||||
+2
-10
@@ -872,12 +872,12 @@ public class PostgresProtocolHandler {
|
||||
MessageDigest md5 = Digests.md5Digest();
|
||||
md5.update(password.getBytes(StandardCharsets.UTF_8));
|
||||
md5.update(username.getBytes(StandardCharsets.UTF_8));
|
||||
String hex1 = bytesToHex(md5.digest());
|
||||
String hex1 = HexFormat.of().formatHex(md5.digest());
|
||||
|
||||
md5.reset();
|
||||
md5.update(hex1.getBytes(StandardCharsets.UTF_8));
|
||||
md5.update(salt);
|
||||
return "md5" + bytesToHex(md5.digest());
|
||||
return "md5" + HexFormat.of().formatHex(md5.digest());
|
||||
}
|
||||
|
||||
// ── Post-auth buffering ───────────────────────────────────────────────────
|
||||
@@ -1685,14 +1685,6 @@ public class PostgresProtocolHandler {
|
||||
|
||||
// ── Wire helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
private static String bytesToHex(byte[] bytes) {
|
||||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static void closeQuietly(Socket s) {
|
||||
try { s.close(); } catch (IOException ignored) {}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Arrays;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -343,12 +344,7 @@ public class PreSignedUrlGenerator {
|
||||
try {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
||||
byte[] hash = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : hash) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
return HexFormat.of().formatHex(mac.doFinal(data.getBytes(StandardCharsets.UTF_8)));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to compute HMAC-SHA256", e);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.security.MessageDigest;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.format.ResolverStyle;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -94,7 +95,7 @@ final class S3PostPolicySigner {
|
||||
String region = parts[2];
|
||||
String service = parts[3];
|
||||
byte[] signingKey = deriveSigningKey(secretKey, date, region, service);
|
||||
String expected = hexEncode(hmacSha256(signingKey, policyBase64));
|
||||
String expected = HexFormat.of().formatHex(hmacSha256(signingKey, policyBase64));
|
||||
return MessageDigest.isEqual(
|
||||
expected.getBytes(StandardCharsets.UTF_8),
|
||||
signatureHex.getBytes(StandardCharsets.UTF_8));
|
||||
@@ -117,12 +118,4 @@ final class S3PostPolicySigner {
|
||||
mac.init(new SecretKeySpec(key, "HmacSHA256"));
|
||||
return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String hexEncode(byte[] bytes) {
|
||||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@@ -54,7 +55,6 @@ public class SesTenantService implements SesTaggable {
|
||||
|
||||
private static final int TENANT_NAME_MAX = 64;
|
||||
private static final Pattern TENANT_NAME_CHARS = Pattern.compile("[A-Za-z0-9_-]+");
|
||||
private static final char[] HEX = "0123456789abcdef".toCharArray();
|
||||
|
||||
// The wire values for ResourceType — the ARN segment, not the SDK enum spelling. Real AWS both
|
||||
// returns these and requires them as Filter values; the SDK's EMAIL_IDENTITY-style enum constants
|
||||
@@ -712,11 +712,7 @@ public class SesTenantService implements SesTaggable {
|
||||
// AWS tenant ids look like "tn-" followed by 30 lowercase hex characters.
|
||||
byte[] bytes = new byte[15];
|
||||
random.nextBytes(bytes);
|
||||
StringBuilder sb = new StringBuilder("tn-");
|
||||
for (byte b : bytes) {
|
||||
sb.append(HEX[(b >> 4) & 0xF]).append(HEX[b & 0xF]);
|
||||
}
|
||||
return sb.toString();
|
||||
return "tn-" + HexFormat.of().formatHex(bytes);
|
||||
}
|
||||
|
||||
// The store is account-scoped transparently by AccountAwareStorageBackend (StorageFactory wraps
|
||||
|
||||
Reference in new issue
Block a user