Skip to content

Commit 319835d

Browse files
committed
feat: Support for converting JWK to public key.
1 parent d081573 commit 319835d

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.danubetech.keyformats;
2+
3+
import bbs.signatures.KeyPair;
4+
import com.danubetech.keyformats.jose.Curve;
5+
import com.danubetech.keyformats.jose.JWK;
6+
import com.danubetech.keyformats.jose.KeyType;
7+
import com.danubetech.keyformats.jose.KeyTypeName;
8+
import com.danubetech.keyformats.keytypes.KeyTypeName_for_JWK;
9+
import org.bitcoinj.core.ECKey;
10+
11+
import java.math.BigInteger;
12+
import java.security.KeyFactory;
13+
import java.security.interfaces.RSAPrivateKey;
14+
import java.security.interfaces.RSAPublicKey;
15+
import java.security.spec.RSAPrivateKeySpec;
16+
import java.security.spec.RSAPublicKeySpec;
17+
18+
public class JWK_to_PublicKey {
19+
20+
public static Object JWK_to_anyPublicKey(JWK jsonWebKey) {
21+
22+
KeyTypeName keyType = KeyTypeName_for_JWK.keyTypeName_for_JWK(jsonWebKey);
23+
24+
if (keyType == KeyTypeName.RSA)
25+
return JWK_to_RSAPublicKey(jsonWebKey);
26+
else if (keyType == KeyTypeName.secp256k1)
27+
return JWK_to_secp256k1PublicKey(jsonWebKey);
28+
else if (keyType == KeyTypeName.BLS12381_G1)
29+
return JWK_to_BLS12381_G2PublicKeyBytes(jsonWebKey);
30+
else if (keyType == KeyTypeName.BLS12381_G2)
31+
return JWK_to_BLS12381_G2PublicKeyBytes(jsonWebKey);
32+
else if (keyType == KeyTypeName.Ed25519)
33+
return JWK_to_Ed25519PublicKeyBytes(jsonWebKey);
34+
else if (keyType == KeyTypeName.X25519)
35+
return JWK_to_X25519PublicKeyBytes(jsonWebKey);
36+
else
37+
throw new IllegalArgumentException("Unsupported key type: " + keyType);
38+
}
39+
40+
public static RSAPublicKey JWK_to_RSAPublicKey(JWK jsonWebKey) {
41+
42+
if (! KeyType.RSA.equals(jsonWebKey.getKty())) throw new IllegalArgumentException("Incorrect key type: " + jsonWebKey.getKty());
43+
44+
try {
45+
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
46+
RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(new BigInteger(jsonWebKey.getNdecoded()), new BigInteger(jsonWebKey.getEdecoded()));
47+
return (RSAPublicKey) keyFactory.generatePrivate(rsaPublicKeySpec);
48+
} catch (Exception ex) {
49+
throw new RuntimeException(ex.getMessage(), ex);
50+
}
51+
}
52+
53+
public static byte[] JWK_to_RSAPublicKeyBytes(JWK jsonWebKey) {
54+
55+
if (! KeyType.RSA.equals(jsonWebKey.getKty())) throw new IllegalArgumentException("Incorrect key type: " + jsonWebKey.getKty());
56+
57+
return JWK_to_RSAPublicKey(jsonWebKey).getEncoded();
58+
}
59+
60+
public static ECKey JWK_to_secp256k1PublicKey(JWK jsonWebKey) {
61+
62+
if (! KeyType.EC.equals(jsonWebKey.getKty())) throw new IllegalArgumentException("Incorrect key type: " + jsonWebKey.getKty());
63+
if (! Curve.secp256k1.equals(jsonWebKey.getCrv())) throw new IllegalArgumentException("Incorrect curve: " + jsonWebKey.getCrv());
64+
65+
return ECKey.fromPublicOnly(jsonWebKey.getXdecoded());
66+
}
67+
68+
public static byte[] JWK_to_secp256k1PublicKeyBytes(JWK jsonWebKey) {
69+
70+
if (! KeyType.EC.equals(jsonWebKey.getKty())) throw new IllegalArgumentException("Incorrect key type: " + jsonWebKey.getKty());
71+
if (! Curve.secp256k1.equals(jsonWebKey.getCrv())) throw new IllegalArgumentException("Incorrect curve: " + jsonWebKey.getCrv());
72+
73+
return jsonWebKey.getXdecoded();
74+
}
75+
76+
public static byte[] JWK_to_BLS12381_G1PublicKeyBytes(JWK jsonWebKey) {
77+
78+
if (! KeyType.EC.equals(jsonWebKey.getKty())) throw new IllegalArgumentException("Incorrect key type: " + jsonWebKey.getKty());
79+
if (! Curve.BLS12381_G1.equals(jsonWebKey.getCrv())) throw new IllegalArgumentException("Incorrect curve: " + jsonWebKey.getCrv());
80+
81+
return jsonWebKey.getXdecoded();
82+
}
83+
84+
public static byte[] JWK_to_BLS12381_G2PublicKeyBytes(JWK jsonWebKey) {
85+
86+
if (! KeyType.EC.equals(jsonWebKey.getKty())) throw new IllegalArgumentException("Incorrect key type: " + jsonWebKey.getKty());
87+
if (! Curve.BLS12381_G2.equals(jsonWebKey.getCrv())) throw new IllegalArgumentException("Incorrect curve: " + jsonWebKey.getCrv());
88+
89+
return jsonWebKey.getXdecoded();
90+
}
91+
92+
public static byte[] JWK_to_Ed25519PublicKeyBytes(JWK jsonWebKey) {
93+
94+
if (! KeyType.OKP.equals(jsonWebKey.getKty())) throw new IllegalArgumentException("Incorrect key type: " + jsonWebKey.getKty());
95+
if (! Curve.Ed25519.equals(jsonWebKey.getCrv())) throw new IllegalArgumentException("Incorrect curve: " + jsonWebKey.getCrv());
96+
97+
return jsonWebKey.getXdecoded();
98+
}
99+
100+
public static byte[] JWK_to_X25519PublicKeyBytes(JWK jsonWebKey) {
101+
102+
if (! KeyType.OKP.equals(jsonWebKey.getKty())) throw new IllegalArgumentException("Incorrect key type: " + jsonWebKey.getKty());
103+
if (! Curve.X25519.equals(jsonWebKey.getCrv())) throw new IllegalArgumentException("Incorrect curve: " + jsonWebKey.getCrv());
104+
105+
return jsonWebKey.getXdecoded();
106+
}
107+
}

0 commit comments

Comments
 (0)