mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-15 13:49:42 +02:00
HPKEParameterSpec.toString with human-readable info
This commit is contained in:
parent
5b4767b106
commit
008a5a6f5b
2 changed files with 30 additions and 2 deletions
|
@ -26,6 +26,7 @@ package javax.crypto.spec;
|
|||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.AsymmetricKey;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.Key;
|
||||
|
@ -399,9 +400,27 @@ public final class HPKEParameterSpec implements AlgorithmParameterSpec {
|
|||
"kem_id=" + kem_id +
|
||||
", kdf_id=" + kdf_id +
|
||||
", aead_id=" + aead_id +
|
||||
", info=" + HexFormat.of().formatHex(info) +
|
||||
", info=" + bytesToString(info) +
|
||||
", " + (psk == null
|
||||
? (kS == null ? "mode_base" : "mode_auth")
|
||||
: (kS == null ? "mode_psk" : "mode_auth_psk")) + "}";
|
||||
}
|
||||
|
||||
// Returns a human-readable format of a byte array.
|
||||
private static String bytesToString(byte[] input) {
|
||||
if (input.length == 0) {
|
||||
return "(empty)";
|
||||
} else {
|
||||
for (byte b : input) {
|
||||
// Returns HEX + string for pure ASCII with no control characters.
|
||||
// Cannot contain `"` to avoid character escaping.
|
||||
if (b < 0x20 || b > 0x7E || b == '"') {
|
||||
return HexFormat.of().formatHex(input);
|
||||
}
|
||||
}
|
||||
// Otherwise, only returns HEX.
|
||||
return HexFormat.of().formatHex(input)
|
||||
+ " (\"" + new String(input, StandardCharsets.US_ASCII) + "\")";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -99,7 +99,16 @@ public class Compliance {
|
|||
Asserts.assertTrue(spec.authKey(kp.getPrivate()).authKey(null).authKey() == null);
|
||||
|
||||
Asserts.assertTrue(spec.toString().contains("kem_id=32, kdf_id=1, aead_id=2"));
|
||||
Asserts.assertTrue(spec.info(new byte[3]).toString().contains("info=000000"));
|
||||
Asserts.assertTrue(spec.toString().contains("info=(empty),"));
|
||||
Asserts.assertTrue(spec.info(new byte[3]).toString().contains("info=000000,"));
|
||||
Asserts.assertTrue(spec.info("info".getBytes(StandardCharsets.UTF_8))
|
||||
.toString().contains("info=696e666f (\"info\"),"));
|
||||
Asserts.assertTrue(spec.info("\"info\"".getBytes(StandardCharsets.UTF_8))
|
||||
.toString().contains("info=22696e666f22,"));
|
||||
Asserts.assertTrue(spec.info("'info'".getBytes(StandardCharsets.UTF_8))
|
||||
.toString().contains("info=27696e666f27 (\"'info'\"),"));
|
||||
Asserts.assertTrue(spec.info("i\\n\\f\\o".getBytes(StandardCharsets.UTF_8))
|
||||
.toString().contains("info=695c6e5c665c6f (\"i\\n\\f\\o\"),"));
|
||||
Asserts.assertTrue(spec.toString().contains("mode_base}"));
|
||||
Asserts.assertTrue(spec.psk(psk, psk_id).toString().contains("mode_psk}"));
|
||||
Asserts.assertTrue(spec.authKey(kp.getPrivate()).toString().contains("mode_auth}"));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue