@@ -42,57 +42,22 @@ public byte[] generateSecret(final PrivateKey privateKey, final PublicKey public
42
42
43
43
@ Override
44
44
public byte [] serializePublicKey (final PublicKey publicKey ) {
45
- // This is a little hacky, but the structure for an X.509 public key defines the order in which its elements appear.
46
- // The first part of the key, which defines the algorithm and its parameters, is always the same for keys of the
47
- // same type, and the last N bytes are the literal key material.
48
- final byte [] serializedPublicKey = new byte [getPublicKeyLength ()];
49
- System .arraycopy (publicKey .getEncoded (), getX509Prefix ().length , serializedPublicKey , 0 , getPublicKeyLength ());
50
-
51
- return serializedPublicKey ;
45
+ return XECUtil .serializePublicKey (publicKey , getPublicKeyLength (), getX509Prefix ());
52
46
}
53
47
54
48
@ Override
55
49
public PublicKey deserializePublicKey (final byte [] publicKeyBytes ) {
56
- final int publicKeyLength = getPublicKeyLength ();
57
-
58
- if (publicKeyBytes .length != publicKeyLength ) {
59
- throw new IllegalArgumentException ("Unexpected serialized public key length" );
60
- }
61
-
62
- final byte [] x509Prefix = getX509Prefix ();
63
- final byte [] x509Bytes = new byte [publicKeyLength + x509Prefix .length ];
64
- System .arraycopy (x509Prefix , 0 , x509Bytes , 0 , x509Prefix .length );
65
- System .arraycopy (publicKeyBytes , 0 , x509Bytes , x509Prefix .length , publicKeyLength );
66
-
67
- try {
68
- return keyFactory .generatePublic (new X509EncodedKeySpec (x509Bytes , keyFactory .getAlgorithm ()));
69
- } catch (final InvalidKeySpecException e ) {
70
- throw new IllegalArgumentException ("Invalid key" , e );
71
- }
50
+ return XECUtil .deserializePublicKey (publicKeyBytes , getPublicKeyLength (), getX509Prefix (), keyFactory );
72
51
}
73
52
74
53
@ Override
75
54
public void checkPublicKey (final PublicKey publicKey ) throws InvalidKeyException {
76
- checkKey (publicKey );
55
+ XECUtil . checkKey (publicKey , keyAgreement . getAlgorithm () );
77
56
}
78
57
79
58
@ Override
80
59
public void checkKeyPair (final KeyPair keyPair ) throws InvalidKeyException {
81
- checkKey (keyPair .getPublic ());
82
- checkKey (keyPair .getPrivate ());
83
- }
84
-
85
- private void checkKey (final Key key ) throws InvalidKeyException {
86
- if (key instanceof XECKey xecKey ) {
87
- if (xecKey .getParams () instanceof NamedParameterSpec namedParameterSpec ) {
88
- if (!keyAgreement .getAlgorithm ().equals (namedParameterSpec .getName ())) {
89
- throw new InvalidKeyException ("Unexpected key algorithm: " + namedParameterSpec .getName ());
90
- }
91
- } else {
92
- throw new InvalidKeyException ("Unexpected key parameter type: " + xecKey .getParams ().getClass ());
93
- }
94
- } else {
95
- throw new InvalidKeyException ("Unexpected key type: " + key .getClass ());
96
- }
60
+ XECUtil .checkKey (keyPair .getPublic (), keyAgreement .getAlgorithm ());
61
+ XECUtil .checkKey (keyPair .getPrivate (), keyAgreement .getAlgorithm ());
97
62
}
98
63
}
0 commit comments