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

@ -1445,7 +1445,7 @@ public final class Main {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
boolean canRead = false;
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
while (true) {
String s = reader.readLine();
if (s == null) break;
@ -2597,7 +2597,7 @@ public final class Main {
throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
boolean started = false;
while (true) {
String s = reader.readLine();
@ -3507,33 +3507,6 @@ public final class Main {
}
}
/**
* Converts a byte to hex digit and writes to the supplied buffer
*/
private void byte2hex(byte b, StringBuffer buf) {
char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
int high = ((b & 0xf0) >> 4);
int low = (b & 0x0f);
buf.append(hexChars[high]);
buf.append(hexChars[low]);
}
/**
* Converts a byte array to hex string
*/
private String toHexString(byte[] block) {
StringBuffer buf = new StringBuffer();
int len = block.length;
for (int i = 0; i < len; i++) {
byte2hex(block[i], buf);
if (i < len-1) {
buf.append(":");
}
}
return buf.toString();
}
/**
* Recovers (private) key associated with given alias.
*
@ -3663,7 +3636,7 @@ public final class Main {
byte[] encCertInfo = cert.getEncoded();
MessageDigest md = MessageDigest.getInstance(mdAlg);
byte[] digest = md.digest(encCertInfo);
return toHexString(digest);
return HexFormat.ofDelimiter(":").withUpperCase().formatHex(digest);
}
/**
@ -4571,21 +4544,16 @@ public final class Main {
break;
case -1:
ObjectIdentifier oid = ObjectIdentifier.of(name);
HexFormat hexFmt = HexFormat.of();
byte[] data = null;
if (value != null) {
data = new byte[value.length() / 2 + 1];
int pos = 0;
for (char c: value.toCharArray()) {
int hex;
if (c >= '0' && c <= '9') {
hex = c - '0' ;
} else if (c >= 'A' && c <= 'F') {
hex = c - 'A' + 10;
} else if (c >= 'a' && c <= 'f') {
hex = c - 'a' + 10;
} else {
if (!hexFmt.isHexDigit(c)) {
continue;
}
int hex = hexFmt.fromHexDigit(c);
if (pos % 2 == 0) {
data[pos/2] = (byte)(hex << 4);
} else {