Skip to content

Commit 68f2acb

Browse files
author
Roger Riggs
committed
8252055: Use java.util.HexFormat in java.security
Reviewed-by: xuelei
1 parent 1dae45d commit 68f2acb

15 files changed

+71
-241
lines changed

src/java.base/share/classes/sun/security/provider/AbstractDrbg.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -730,19 +730,6 @@ private static synchronized byte[] next() {
730730

731731
// Misc
732732

733-
/** A handy method returning hexdump string with no colon or new line.
734-
*
735-
* @param in input byte array
736-
* @return the hexdump string
737-
*/
738-
protected static String hex(byte[] in) {
739-
StringBuilder sb = new StringBuilder();
740-
for (byte b : in) {
741-
sb.append(String.format("%02x", b&0xff));
742-
}
743-
return sb.toString();
744-
}
745-
746733
/**
747734
* Returns the smallest standard strength (112, 128, 192, 256) that is
748735
* greater or equal to the input.

src/java.base/share/classes/sun/security/provider/AbstractHashDrbg.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it

src/java.base/share/classes/sun/security/provider/CtrDrbg.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,7 @@
3030
import javax.crypto.spec.SecretKeySpec;
3131
import java.security.*;
3232
import java.util.Arrays;
33+
import java.util.HexFormat;
3334
import java.util.Locale;
3435

3536
public class CtrDrbg extends AbstractDrbg {
@@ -181,8 +182,8 @@ protected void initEngine() {
181182

182183
private void status() {
183184
if (debug != null) {
184-
debug.println(this, "Key = " + hex(k));
185-
debug.println(this, "V = " + hex(v));
185+
debug.println(this, "Key = " + HexFormat.of().formatHex(k));
186+
debug.println(this, "V = " + HexFormat.of().formatHex(v));
186187
debug.println(this, "reseed counter = " + reseedCounter);
187188
}
188189
}

src/java.base/share/classes/sun/security/provider/HashDrbg.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,6 +33,7 @@
3333
import java.security.SecureRandomParameters;
3434
import java.util.ArrayList;
3535
import java.util.Arrays;
36+
import java.util.HexFormat;
3637
import java.util.List;
3738

3839
public class HashDrbg extends AbstractHashDrbg {
@@ -161,8 +162,8 @@ protected final synchronized void hashReseedInternal(List<byte[]> inputs) {
161162

162163
private void status() {
163164
if (debug != null) {
164-
debug.println(this, "V = " + hex(v));
165-
debug.println(this, "C = " + hex(c));
165+
debug.println(this, "V = " + HexFormat.of().formatHex(v));
166+
debug.println(this, "C = " + HexFormat.of().formatHex(c));
166167
debug.println(this, "reseed counter = " + reseedCounter);
167168
}
168169
}

src/java.base/share/classes/sun/security/provider/HmacDrbg.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,6 +33,7 @@
3333
import java.security.SecureRandomParameters;
3434
import java.util.Arrays;
3535
import java.util.Collections;
36+
import java.util.HexFormat;
3637
import java.util.List;
3738

3839
public class HmacDrbg extends AbstractHashDrbg {
@@ -51,8 +52,8 @@ public HmacDrbg(SecureRandomParameters params) {
5152

5253
private void status() {
5354
if (debug != null) {
54-
debug.println(this, "V = " + hex(v));
55-
debug.println(this, "Key = " + hex(k));
55+
debug.println(this, "V = " + HexFormat.of().formatHex(v));
56+
debug.println(this, "Key = " + HexFormat.of().formatHex(k));
5657
debug.println(this, "reseed counter = " + reseedCounter);
5758
}
5859
}

src/java.base/share/classes/sun/security/provider/certpath/RevocationChecker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,12 +774,12 @@ private void checkOCSP(X509Certificate cert,
774774
/*
775775
* Removes any non-hexadecimal characters from a string.
776776
*/
777-
private static final String HEX_DIGITS = "0123456789ABCDEFabcdef";
778777
private static String stripOutSeparators(String value) {
778+
HexFormat hex = HexFormat.of();
779779
char[] chars = value.toCharArray();
780780
StringBuilder hexNumber = new StringBuilder();
781781
for (int i = 0; i < chars.length; i++) {
782-
if (HEX_DIGITS.indexOf(chars[i]) != -1) {
782+
if (hex.isHexDigit(chars[i])) {
783783
hexNumber.append(chars[i]);
784784
}
785785
}

src/java.base/share/classes/sun/security/ssl/SSLLogger.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.time.Instant;
4040
import java.time.ZoneId;
4141
import java.time.format.DateTimeFormatter;
42+
import java.util.HexFormat;
4243
import java.util.Locale;
4344
import java.util.Map;
4445
import java.util.ResourceBundle;
@@ -580,7 +581,7 @@ private static String formatMapEntry(Map.Entry<String, ?> entry) {
580581
Utilities.toHexString((byte[])value) + "\"";
581582
} else if (value instanceof Byte) {
582583
formatted = "\"" + key + "\": \"" +
583-
Utilities.toHexString((byte)value) + "\"";
584+
HexFormat.of().toHexDigits((byte)value) + "\"";
584585
} else {
585586
formatted = "\"" + key + "\": " +
586587
"\"" + value.toString() + "\"";

src/java.base/share/classes/sun/security/ssl/ServerHello.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.text.MessageFormat;
3434
import java.util.Arrays;
3535
import java.util.EnumSet;
36+
import java.util.HexFormat;
3637
import java.util.LinkedList;
3738
import java.util.List;
3839
import java.util.Locale;
@@ -237,9 +238,8 @@ public String toString() {
237238
serverVersion.name,
238239
Utilities.toHexString(serverRandom.randomBytes),
239240
sessionId.toString(),
240-
cipherSuite.name + "(" +
241-
Utilities.byte16HexString(cipherSuite.id) + ")",
242-
Utilities.toHexString(compressionMethod),
241+
cipherSuite.name + "(" + Utilities.byte16HexString(cipherSuite.id) + ")",
242+
HexFormat.of().toHexDigits(compressionMethod),
243243
Utilities.indent(extensions.toString(), " ")
244244
};
245245

src/java.base/share/classes/sun/security/ssl/Utilities.java

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,10 +36,11 @@
3636
* A utility class to share the static methods.
3737
*/
3838
final class Utilities {
39-
static final char[] hexDigits = "0123456789ABCDEF".toCharArray();
4039
private static final String indent = " ";
4140
private static final Pattern lineBreakPatern =
4241
Pattern.compile("\\r\\n|\\n|\\r");
42+
private static final HexFormat HEX_FORMATTER =
43+
HexFormat.of().withUpperCase();
4344

4445
/**
4546
* Puts {@code hostname} into the {@code serverNames} list.
@@ -164,35 +165,16 @@ static String indent(String source, String prefix) {
164165
return builder.toString();
165166
}
166167

167-
static String toHexString(byte b) {
168-
return String.valueOf(hexDigits[(b >> 4) & 0x0F]) +
169-
String.valueOf(hexDigits[b & 0x0F]);
170-
}
171-
172168
static String byte16HexString(int id) {
173-
return "0x" +
174-
hexDigits[(id >> 12) & 0x0F] + hexDigits[(id >> 8) & 0x0F] +
175-
hexDigits[(id >> 4) & 0x0F] + hexDigits[id & 0x0F];
169+
return "0x" + HEX_FORMATTER.toHexDigits((short)id);
176170
}
177171

178172
static String toHexString(byte[] bytes) {
179173
if (bytes == null || bytes.length == 0) {
180174
return "";
181175
}
182176

183-
StringBuilder builder = new StringBuilder(bytes.length * 3);
184-
boolean isFirst = true;
185-
for (byte b : bytes) {
186-
if (isFirst) {
187-
isFirst = false;
188-
} else {
189-
builder.append(' ');
190-
}
191-
192-
builder.append(hexDigits[(b >> 4) & 0x0F]);
193-
builder.append(hexDigits[b & 0x0F]);
194-
}
195-
return builder.toString();
177+
return HEX_FORMATTER.formatHex(bytes);
196178
}
197179

198180
static String toHexString(long lv) {
@@ -206,10 +188,8 @@ static String toHexString(long lv) {
206188
builder.append(' ');
207189
}
208190

209-
builder.append(hexDigits[(int)(lv & 0x0F)]);
210-
lv >>>= 4;
211-
builder.append(hexDigits[(int)(lv & 0x0F)]);
212-
lv >>>= 4;
191+
HEX_FORMATTER.toHexDigits(builder, (byte)lv);
192+
lv >>>= 8;
213193
} while (lv != 0);
214194
builder.reverse();
215195

src/java.base/share/classes/sun/security/tools/keytool/Main.java

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ private void doGenCert(String alias, String sigAlgName, InputStream in, PrintStr
14451445

14461446
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
14471447
boolean canRead = false;
1448-
StringBuffer sb = new StringBuffer();
1448+
StringBuilder sb = new StringBuilder();
14491449
while (true) {
14501450
String s = reader.readLine();
14511451
if (s == null) break;
@@ -2597,7 +2597,7 @@ private void doPrintCertReq(InputStream in, PrintStream out)
25972597
throws Exception {
25982598

25992599
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
2600-
StringBuffer sb = new StringBuffer();
2600+
StringBuilder sb = new StringBuilder();
26012601
boolean started = false;
26022602
while (true) {
26032603
String s = reader.readLine();
@@ -3507,33 +3507,6 @@ private void dumpCert(Certificate cert, PrintStream out)
35073507
}
35083508
}
35093509

3510-
/**
3511-
* Converts a byte to hex digit and writes to the supplied buffer
3512-
*/
3513-
private void byte2hex(byte b, StringBuffer buf) {
3514-
char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
3515-
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
3516-
int high = ((b & 0xf0) >> 4);
3517-
int low = (b & 0x0f);
3518-
buf.append(hexChars[high]);
3519-
buf.append(hexChars[low]);
3520-
}
3521-
3522-
/**
3523-
* Converts a byte array to hex string
3524-
*/
3525-
private String toHexString(byte[] block) {
3526-
StringBuffer buf = new StringBuffer();
3527-
int len = block.length;
3528-
for (int i = 0; i < len; i++) {
3529-
byte2hex(block[i], buf);
3530-
if (i < len-1) {
3531-
buf.append(":");
3532-
}
3533-
}
3534-
return buf.toString();
3535-
}
3536-
35373510
/**
35383511
* Recovers (private) key associated with given alias.
35393512
*
@@ -3663,7 +3636,7 @@ private String getCertFingerPrint(String mdAlg, Certificate cert)
36633636
byte[] encCertInfo = cert.getEncoded();
36643637
MessageDigest md = MessageDigest.getInstance(mdAlg);
36653638
byte[] digest = md.digest(encCertInfo);
3666-
return toHexString(digest);
3639+
return HexFormat.ofDelimiter(":").withUpperCase().formatHex(digest);
36673640
}
36683641

36693642
/**
@@ -4571,21 +4544,16 @@ private CertificateExtensions createV3Extensions(
45714544
break;
45724545
case -1:
45734546
ObjectIdentifier oid = ObjectIdentifier.of(name);
4547+
HexFormat hexFmt = HexFormat.of();
45744548
byte[] data = null;
45754549
if (value != null) {
45764550
data = new byte[value.length() / 2 + 1];
45774551
int pos = 0;
45784552
for (char c: value.toCharArray()) {
4579-
int hex;
4580-
if (c >= '0' && c <= '9') {
4581-
hex = c - '0' ;
4582-
} else if (c >= 'A' && c <= 'F') {
4583-
hex = c - 'A' + 10;
4584-
} else if (c >= 'a' && c <= 'f') {
4585-
hex = c - 'a' + 10;
4586-
} else {
4553+
if (!hexFmt.isHexDigit(c)) {
45874554
continue;
45884555
}
4556+
int hex = hexFmt.fromHexDigit(c);
45894557
if (pos % 2 == 0) {
45904558
data[pos/2] = (byte)(hex << 4);
45914559
} else {

src/java.base/share/classes/sun/security/util/Debug.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@
2727

2828
import java.io.PrintStream;
2929
import java.math.BigInteger;
30+
import java.util.HexFormat;
3031
import java.util.regex.Pattern;
3132
import java.util.regex.Matcher;
3233
import java.util.Locale;
@@ -318,22 +319,11 @@ private static String marshal(String args) {
318319
return null;
319320
}
320321

321-
private static final char[] hexDigits = "0123456789abcdef".toCharArray();
322-
323322
public static String toString(byte[] b) {
324323
if (b == null) {
325324
return "(null)";
326325
}
327-
StringBuilder sb = new StringBuilder(b.length * 3);
328-
for (int i = 0; i < b.length; i++) {
329-
int k = b[i] & 0xff;
330-
if (i != 0) {
331-
sb.append(':');
332-
}
333-
sb.append(hexDigits[k >>> 4]);
334-
sb.append(hexDigits[k & 0xf]);
335-
}
336-
return sb.toString();
326+
return HexFormat.ofDelimiter(":").formatHex(b);
337327
}
338328

339329
}

src/java.base/share/classes/sun/security/util/ManifestEntryVerifier.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -214,8 +214,8 @@ public CodeSigner[] verify(Hashtable<String, CodeSigner[]> verifiedSigners,
214214
if (debug != null) {
215215
debug.println("Manifest Entry: " +
216216
name + " digest=" + digest.getAlgorithm());
217-
debug.println(" manifest " + toHex(manHash));
218-
debug.println(" computed " + toHex(theHash));
217+
debug.println(" manifest " + HexFormat.of().formatHex(manHash));
218+
debug.println(" computed " + HexFormat.of().formatHex(theHash));
219219
debug.println();
220220
}
221221

@@ -231,25 +231,4 @@ public CodeSigner[] verify(Hashtable<String, CodeSigner[]> verifiedSigners,
231231
}
232232
return signers;
233233
}
234-
235-
// for the toHex function
236-
private static final char[] hexc =
237-
{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
238-
/**
239-
* convert a byte array to a hex string for debugging purposes
240-
* @param data the binary data to be converted to a hex string
241-
* @return an ASCII hex string
242-
*/
243-
244-
static String toHex(byte[] data) {
245-
246-
StringBuilder sb = new StringBuilder(data.length*2);
247-
248-
for (int i=0; i<data.length; i++) {
249-
sb.append(hexc[(data[i] >>4) & 0x0f]);
250-
sb.append(hexc[data[i] & 0x0f]);
251-
}
252-
return sb.toString();
253-
}
254-
255234
}

0 commit comments

Comments
 (0)