8252055: Use java.util.HexFormat in java.security

Reviewed-by: xuelei
This commit is contained in:
Roger Riggs 2020-12-18 16:35:11 +00:00
parent 1dae45d745
commit 68f2acbf4c
15 changed files with 71 additions and 241 deletions

View file

@ -39,6 +39,7 @@ import java.text.MessageFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HexFormat;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
@ -580,7 +581,7 @@ public final class SSLLogger {
Utilities.toHexString((byte[])value) + "\"";
} else if (value instanceof Byte) {
formatted = "\"" + key + "\": \"" +
Utilities.toHexString((byte)value) + "\"";
HexFormat.of().toHexDigits((byte)value) + "\"";
} else {
formatted = "\"" + key + "\": " +
"\"" + value.toString() + "\"";

View file

@ -33,6 +33,7 @@ import java.security.GeneralSecurityException;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HexFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
@ -237,9 +238,8 @@ final class ServerHello {
serverVersion.name,
Utilities.toHexString(serverRandom.randomBytes),
sessionId.toString(),
cipherSuite.name + "(" +
Utilities.byte16HexString(cipherSuite.id) + ")",
Utilities.toHexString(compressionMethod),
cipherSuite.name + "(" + Utilities.byte16HexString(cipherSuite.id) + ")",
HexFormat.of().toHexDigits(compressionMethod),
Utilities.indent(extensions.toString(), " ")
};

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -36,10 +36,11 @@ import sun.security.action.GetPropertyAction;
* A utility class to share the static methods.
*/
final class Utilities {
static final char[] hexDigits = "0123456789ABCDEF".toCharArray();
private static final String indent = " ";
private static final Pattern lineBreakPatern =
Pattern.compile("\\r\\n|\\n|\\r");
private static final HexFormat HEX_FORMATTER =
HexFormat.of().withUpperCase();
/**
* Puts {@code hostname} into the {@code serverNames} list.
@ -164,15 +165,8 @@ final class Utilities {
return builder.toString();
}
static String toHexString(byte b) {
return String.valueOf(hexDigits[(b >> 4) & 0x0F]) +
String.valueOf(hexDigits[b & 0x0F]);
}
static String byte16HexString(int id) {
return "0x" +
hexDigits[(id >> 12) & 0x0F] + hexDigits[(id >> 8) & 0x0F] +
hexDigits[(id >> 4) & 0x0F] + hexDigits[id & 0x0F];
return "0x" + HEX_FORMATTER.toHexDigits((short)id);
}
static String toHexString(byte[] bytes) {
@ -180,19 +174,7 @@ final class Utilities {
return "";
}
StringBuilder builder = new StringBuilder(bytes.length * 3);
boolean isFirst = true;
for (byte b : bytes) {
if (isFirst) {
isFirst = false;
} else {
builder.append(' ');
}
builder.append(hexDigits[(b >> 4) & 0x0F]);
builder.append(hexDigits[b & 0x0F]);
}
return builder.toString();
return HEX_FORMATTER.formatHex(bytes);
}
static String toHexString(long lv) {
@ -206,10 +188,8 @@ final class Utilities {
builder.append(' ');
}
builder.append(hexDigits[(int)(lv & 0x0F)]);
lv >>>= 4;
builder.append(hexDigits[(int)(lv & 0x0F)]);
lv >>>= 4;
HEX_FORMATTER.toHexDigits(builder, (byte)lv);
lv >>>= 8;
} while (lv != 0);
builder.reverse();