Skip to content

Commit a8157fe

Browse files
committed
Ensure Digest.digestAsHex() has a fixed width value.
1 parent 307aab1 commit a8157fe

File tree

1 file changed

+12
-1
lines changed
  • compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/util

1 file changed

+12
-1
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/util/Digest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,18 @@ private static void encodeBase62(long value, byte[] result, int resultIndex) {
123123
public static String digestAsHex(String value) {
124124
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
125125
LongLong hash = MurmurHash3_x64_128(bytes, 0, bytes.length, HASH_SEED);
126-
return Long.toHexString(hash.l1) + Long.toHexString(hash.l2);
126+
String asHex = toFixedSizeHexString(hash.l1) + toFixedSizeHexString(hash.l2);
127+
GraalError.guarantee(asHex.length() == 32, "Expecting a 32 digits long hex value.");
128+
return asHex;
129+
}
130+
131+
/**
132+
* Note: The string generated by {@link Long#toHexString(long)} can vary in length, and it
133+
* doesn't include any leading 0s. With {@link String#format(String, Object...)} we inject
134+
* leading 0s when necessary to ensure that the produced digest has a fixed size.
135+
*/
136+
private static String toFixedSizeHexString(long l) {
137+
return String.format("%016x", l);
127138
}
128139

129140
public static UUID digestAsUUID(String value) {

0 commit comments

Comments
 (0)