1
1
package org .bouncycastle .jcajce .provider .asymmetric .mlkem ;
2
2
3
+ import java .io .IOException ;
4
+ import java .io .ObjectInputStream ;
5
+ import java .io .ObjectOutputStream ;
6
+
3
7
import org .bouncycastle .asn1 .x509 .SubjectPublicKeyInfo ;
4
8
import org .bouncycastle .jcajce .interfaces .MLKEMPublicKey ;
5
9
import org .bouncycastle .jcajce .spec .MLKEMParameterSpec ;
6
10
import org .bouncycastle .pqc .crypto .mlkem .MLKEMPublicKeyParameters ;
7
11
import org .bouncycastle .pqc .crypto .util .PublicKeyFactory ;
8
12
import org .bouncycastle .pqc .crypto .util .SubjectPublicKeyInfoFactory ;
9
13
import org .bouncycastle .util .Arrays ;
10
-
11
- import java .io .IOException ;
12
- import java .io .ObjectInputStream ;
13
- import java .io .ObjectOutputStream ;
14
+ import org .bouncycastle .util .Fingerprint ;
15
+ import org .bouncycastle .util .Strings ;
16
+ import org .bouncycastle .util .encoders .Hex ;
14
17
15
18
public class BCMLKEMPublicKey
16
19
implements MLKEMPublicKey
@@ -20,7 +23,6 @@ public class BCMLKEMPublicKey
20
23
private transient MLKEMPublicKeyParameters params ;
21
24
22
25
private transient String algorithm ;
23
- private transient byte [] encoding ;
24
26
25
27
public BCMLKEMPublicKey (
26
28
MLKEMPublicKeyParameters params )
@@ -38,13 +40,13 @@ private void init(SubjectPublicKeyInfo keyInfo)
38
40
throws IOException
39
41
{
40
42
this .params = (MLKEMPublicKeyParameters )PublicKeyFactory .createKey (keyInfo );
41
- this .algorithm = params .getParameters ().getName ();
43
+ this .algorithm = MLKEMParameterSpec . fromName ( params .getParameters ().getName ()). getName (). toUpperCase ();
42
44
}
43
45
44
46
private void init (MLKEMPublicKeyParameters params )
45
47
{
46
48
this .params = params ;
47
- this .algorithm = params .getParameters ().getName ();
49
+ this .algorithm = MLKEMParameterSpec . fromName ( params .getParameters ().getName ()). getName (). toUpperCase ();
48
50
}
49
51
/**
50
52
* Compare this ML-KEM public key with another object.
@@ -82,6 +84,11 @@ public final String getAlgorithm()
82
84
return algorithm ;
83
85
}
84
86
87
+ public byte [] getPublicData ()
88
+ {
89
+ return params .getEncoded ();
90
+ }
91
+
85
92
public byte [] getEncoded ()
86
93
{
87
94
try
@@ -106,7 +113,27 @@ public MLKEMParameterSpec getParameterSpec()
106
113
return MLKEMParameterSpec .fromName (params .getParameters ().getName ());
107
114
}
108
115
109
- public MLKEMPublicKeyParameters getKeyParams ()
116
+ public String toString ()
117
+ {
118
+ StringBuilder buf = new StringBuilder ();
119
+ String nl = Strings .lineSeparator ();
120
+ byte [] keyBytes = params .getEncoded ();
121
+
122
+ // -DM Hex.toHexString
123
+ buf .append (getAlgorithm ())
124
+ .append (" " )
125
+ .append ("Public Key" ).append (" [" )
126
+ .append (new Fingerprint (keyBytes ).toString ())
127
+ .append ("]" )
128
+ .append (nl )
129
+ .append (" public data: " )
130
+ .append (Hex .toHexString (keyBytes ))
131
+ .append (nl );
132
+
133
+ return buf .toString ();
134
+ }
135
+
136
+ MLKEMPublicKeyParameters getKeyParams ()
110
137
{
111
138
return params ;
112
139
}
0 commit comments