mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8252055: Use java.util.HexFormat in java.security
Reviewed-by: xuelei
This commit is contained in:
parent
1dae45d745
commit
68f2acbf4c
15 changed files with 71 additions and 241 deletions
|
@ -108,12 +108,6 @@ public class AVA implements DerEncoder {
|
|||
private static final String specialCharsDefault = ",=\n+<>#;\\\" ";
|
||||
private static final String escapedDefault = ",+<>;\"";
|
||||
|
||||
/*
|
||||
* Values that aren't printable strings are emitted as BER-encoded
|
||||
* hex data.
|
||||
*/
|
||||
private static final String hexDigits = "0123456789ABCDEF";
|
||||
|
||||
public AVA(ObjectIdentifier type, DerValue val) {
|
||||
if ((type == null) || (val == null)) {
|
||||
throw new NullPointerException();
|
||||
|
@ -257,7 +251,7 @@ public class AVA implements DerEncoder {
|
|||
|
||||
private static DerValue parseHexString
|
||||
(Reader in, int format) throws IOException {
|
||||
|
||||
HexFormat hex = HexFormat.of();
|
||||
int c;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
byte b = 0;
|
||||
|
@ -268,21 +262,18 @@ public class AVA implements DerEncoder {
|
|||
if (isTerminator(c, format)) {
|
||||
break;
|
||||
}
|
||||
|
||||
int cVal = hexDigits.indexOf(Character.toUpperCase((char)c));
|
||||
|
||||
if (cVal == -1) {
|
||||
throw new IOException("AVA parse, invalid hex " +
|
||||
"digit: "+ (char)c);
|
||||
try {
|
||||
int cVal = hex.fromHexDigit(c); // throws on invalid character
|
||||
if ((cNdx % 2) == 1) {
|
||||
b = (byte)((b * 16) + (byte)(cVal));
|
||||
baos.write(b);
|
||||
} else {
|
||||
b = (byte)(cVal);
|
||||
}
|
||||
cNdx++;
|
||||
} catch (NumberFormatException nfe) {
|
||||
throw new IOException("AVA parse, invalid hex digit: "+ (char)c);
|
||||
}
|
||||
|
||||
if ((cNdx % 2) == 1) {
|
||||
b = (byte)((b * 16) + (byte)(cVal));
|
||||
baos.write(b);
|
||||
} else {
|
||||
b = (byte)(cVal);
|
||||
}
|
||||
cNdx++;
|
||||
}
|
||||
|
||||
// throw exception if no hex digits
|
||||
|
@ -511,13 +502,14 @@ public class AVA implements DerEncoder {
|
|||
private static Byte getEmbeddedHexPair(int c1, Reader in)
|
||||
throws IOException {
|
||||
|
||||
if (hexDigits.indexOf(Character.toUpperCase((char)c1)) >= 0) {
|
||||
HexFormat hex = HexFormat.of();
|
||||
if (hex.isHexDigit(c1)) {
|
||||
int c2 = readChar(in, "unexpected EOF - " +
|
||||
"escaped hex value must include two valid digits");
|
||||
|
||||
if (hexDigits.indexOf(Character.toUpperCase((char)c2)) >= 0) {
|
||||
int hi = Character.digit((char)c1, 16);
|
||||
int lo = Character.digit((char)c2, 16);
|
||||
if (hex.isHexDigit(c2)) {
|
||||
int hi = hex.fromHexDigit(c1);
|
||||
int lo = hex.fromHexDigit(c2);
|
||||
return (byte)((hi<<4) + lo);
|
||||
} else {
|
||||
throw new IOException
|
||||
|
@ -737,11 +729,7 @@ public class AVA implements DerEncoder {
|
|||
throw new IllegalArgumentException("DER Value conversion");
|
||||
}
|
||||
typeAndValue.append('#');
|
||||
for (int j = 0; j < data.length; j++) {
|
||||
byte b = data[j];
|
||||
typeAndValue.append(Character.forDigit(0xF & (b >>> 4), 16));
|
||||
typeAndValue.append(Character.forDigit(0xF & b, 16));
|
||||
}
|
||||
HexFormat.of().formatHex(typeAndValue, data);
|
||||
} else {
|
||||
/*
|
||||
* 2.4 (cont): Otherwise, if the AttributeValue is of a type which
|
||||
|
@ -806,15 +794,7 @@ public class AVA implements DerEncoder {
|
|||
// embed non-printable/non-escaped char
|
||||
// as escaped hex pairs for debugging
|
||||
byte[] valueBytes = Character.toString(c).getBytes(UTF_8);
|
||||
for (int j = 0; j < valueBytes.length; j++) {
|
||||
sbuffer.append('\\');
|
||||
char hexChar = Character.forDigit
|
||||
(0xF & (valueBytes[j] >>> 4), 16);
|
||||
sbuffer.append(Character.toUpperCase(hexChar));
|
||||
hexChar = Character.forDigit
|
||||
(0xF & (valueBytes[j]), 16);
|
||||
sbuffer.append(Character.toUpperCase(hexChar));
|
||||
}
|
||||
HexFormat.of().withPrefix("\\").withUpperCase().formatHex(sbuffer, valueBytes);
|
||||
} else {
|
||||
|
||||
// append non-printable/non-escaped char
|
||||
|
@ -884,11 +864,7 @@ public class AVA implements DerEncoder {
|
|||
throw new IllegalArgumentException("DER Value conversion");
|
||||
}
|
||||
typeAndValue.append('#');
|
||||
for (int j = 0; j < data.length; j++) {
|
||||
byte b = data[j];
|
||||
typeAndValue.append(Character.forDigit(0xF & (b >>> 4), 16));
|
||||
typeAndValue.append(Character.forDigit(0xF & b, 16));
|
||||
}
|
||||
HexFormat.of().formatHex(typeAndValue, data);
|
||||
} else {
|
||||
/*
|
||||
* 2.4 (cont): Otherwise, if the AttributeValue is of a type which
|
||||
|
@ -960,15 +936,8 @@ public class AVA implements DerEncoder {
|
|||
// as escaped hex pairs for debugging
|
||||
|
||||
previousWhite = false;
|
||||
|
||||
byte[] valueBytes = Character.toString(c).getBytes(UTF_8);
|
||||
for (int j = 0; j < valueBytes.length; j++) {
|
||||
sbuffer.append('\\');
|
||||
sbuffer.append(Character.forDigit
|
||||
(0xF & (valueBytes[j] >>> 4), 16));
|
||||
sbuffer.append(Character.forDigit
|
||||
(0xF & (valueBytes[j]), 16));
|
||||
}
|
||||
HexFormat.of().withPrefix("\\").withUpperCase().formatHex(sbuffer, valueBytes);
|
||||
} else {
|
||||
|
||||
// append non-printable/non-escaped char
|
||||
|
@ -1042,11 +1011,7 @@ public class AVA implements DerEncoder {
|
|||
byte[] data = value.toByteArray();
|
||||
|
||||
retval.append('#');
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
retval.append(hexDigits.charAt((data [i] >> 4) & 0x0f));
|
||||
retval.append(hexDigits.charAt(data [i] & 0x0f));
|
||||
}
|
||||
|
||||
HexFormat.of().formatHex(retval, data);
|
||||
} else {
|
||||
|
||||
boolean quoteNeeded = false;
|
||||
|
@ -1106,15 +1071,7 @@ public class AVA implements DerEncoder {
|
|||
// embed escaped hex pairs
|
||||
byte[] valueBytes =
|
||||
Character.toString(c).getBytes(UTF_8);
|
||||
for (int j = 0; j < valueBytes.length; j++) {
|
||||
sbuffer.append('\\');
|
||||
char hexChar = Character.forDigit
|
||||
(0xF & (valueBytes[j] >>> 4), 16);
|
||||
sbuffer.append(Character.toUpperCase(hexChar));
|
||||
hexChar = Character.forDigit
|
||||
(0xF & (valueBytes[j]), 16);
|
||||
sbuffer.append(Character.toUpperCase(hexChar));
|
||||
}
|
||||
HexFormat.of().withPrefix("\\").withUpperCase().formatHex(sbuffer, valueBytes);
|
||||
} else {
|
||||
|
||||
// append non-printable/non-escaped char
|
||||
|
|
|
@ -1932,26 +1932,10 @@ public class X509CertImpl extends X509Certificate implements DerEncoder {
|
|||
byte[] encCertInfo = cert.getEncoded();
|
||||
MessageDigest md = MessageDigest.getInstance(algorithm);
|
||||
byte[] digest = md.digest(encCertInfo);
|
||||
StringBuilder sb = new StringBuilder(digest.length * 2);
|
||||
for (int i = 0; i < digest.length; i++) {
|
||||
byte2hex(digest[i], sb);
|
||||
}
|
||||
return sb.toString();
|
||||
return HexFormat.of().withUpperCase().formatHex(digest);
|
||||
} catch (NoSuchAlgorithmException | CertificateEncodingException e) {
|
||||
// ignored
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a byte to hex digit and writes to the supplied builder
|
||||
*/
|
||||
private static void byte2hex(byte b, StringBuilder 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])
|
||||
.append(hexChars[low]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue