|
4 | 4 | import java.security.interfaces.RSAPublicKey; |
5 | 5 | import java.util.Arrays; |
6 | 6 |
|
7 | | -import com.danubetech.keyformats.jose.Curves; |
| 7 | +import bbs.signatures.KeyPair; |
| 8 | +import com.danubetech.keyformats.jose.Curve; |
| 9 | +import com.danubetech.keyformats.jose.JWK; |
| 10 | +import com.danubetech.keyformats.jose.KeyType; |
| 11 | +import org.apache.commons.codec.binary.Base64; |
8 | 12 | import org.bitcoinj.core.ECKey; |
9 | 13 | import org.bouncycastle.math.ec.ECPoint; |
10 | 14 |
|
11 | | -import com.nimbusds.jose.jwk.Curve; |
12 | | -import com.nimbusds.jose.jwk.KeyUse; |
13 | | -import com.nimbusds.jose.util.Base64URL; |
14 | | - |
15 | 15 | public class PrivateKey_to_JWK { |
16 | 16 |
|
17 | | - public static com.nimbusds.jose.jwk.RSAKey RSAPrivateKey_to_JWK(RSAPrivateKey privateKey, RSAPublicKey publicKey, String kid, String use) { |
| 17 | + public static JWK RSAPrivateKey_to_JWK(RSAPrivateKey privateKey, RSAPublicKey publicKey, String kid, String use) { |
| 18 | + |
| 19 | + throw new RuntimeException("Not supported"); |
18 | 20 |
|
19 | | - com.nimbusds.jose.jwk.RSAKey jsonWebKey = new com.nimbusds.jose.jwk.RSAKey.Builder(publicKey) |
| 21 | +/* com.nimbusds.jose.jwk.RSAKey jsonWebKey = new com.nimbusds.jose.jwk.RSAKey.Builder(publicKey) |
20 | 22 | .privateKey(privateKey) |
21 | 23 | .keyID(kid) |
22 | 24 | .keyUse(use == null ? null : new KeyUse(use)) |
23 | 25 | .build(); |
24 | 26 |
|
25 | | - return jsonWebKey; |
| 27 | + return jsonWebKey;*/ |
26 | 28 | } |
27 | 29 |
|
28 | | - public static com.nimbusds.jose.jwk.ECKey secp256k1PrivateKey_to_JWK(ECKey privateKey, String kid, String use) { |
| 30 | + public static JWK secp256k1PrivateKey_to_JWK(ECKey privateKey, String kid, String use) { |
29 | 31 |
|
30 | 32 | ECPoint publicKeyPoint = privateKey.getPubKeyPoint(); |
31 | 33 | byte[] privateKeyBytes = privateKey.getPrivKeyBytes(); |
32 | | - Base64URL xParameter = Base64URL.encode(publicKeyPoint.getAffineXCoord().getEncoded()); |
33 | | - Base64URL yParameter = Base64URL.encode(publicKeyPoint.getAffineYCoord().getEncoded()); |
34 | | - Base64URL dParameter = Base64URL.encode(privateKeyBytes); |
35 | 34 |
|
36 | | - com.nimbusds.jose.jwk.ECKey jsonWebKey = new com.nimbusds.jose.jwk.ECKey.Builder(Curve.SECP256K1, xParameter, yParameter) |
37 | | - .d(dParameter) |
38 | | - .keyID(kid) |
39 | | - .keyUse(use == null ? null : new KeyUse(use)) |
40 | | - .build(); |
| 35 | + JWK jsonWebKey = new JWK(); |
| 36 | + jsonWebKey.setKty(KeyType.EC); |
| 37 | + jsonWebKey.setCrv(Curve.secp256k1); |
| 38 | + jsonWebKey.setKid(kid); |
| 39 | + jsonWebKey.setUse(use); |
| 40 | + jsonWebKey.setX(Base64.encodeBase64URLSafeString(publicKeyPoint.getAffineXCoord().getEncoded())); |
| 41 | + jsonWebKey.setY(Base64.encodeBase64URLSafeString(publicKeyPoint.getAffineYCoord().getEncoded())); |
| 42 | + jsonWebKey.setD(Base64.encodeBase64URLSafeString(privateKeyBytes)); |
41 | 43 |
|
42 | 44 | return jsonWebKey; |
43 | 45 | } |
44 | 46 |
|
45 | | - public static com.nimbusds.jose.jwk.ECKey secp256k1PrivateKeyBytes_to_JWK(byte[] privateKeyBytes, String kid, String use) { |
| 47 | + public static JWK secp256k1PrivateKeyBytes_to_JWK(byte[] privateKeyBytes, String kid, String use) { |
46 | 48 |
|
47 | 49 | ECKey privateKey = ECKey.fromPrivate(privateKeyBytes); |
48 | 50 |
|
49 | 51 | return secp256k1PrivateKey_to_JWK(privateKey, kid, use); |
50 | 52 | } |
51 | 53 |
|
52 | | - public static com.nimbusds.jose.jwk.ECKey BLS12381_G1PrivateKey_to_JWK(ECKey privateKey, String kid, String use) { |
| 54 | + public static JWK BLS12381_G1PrivateKey_to_JWK(KeyPair privateKey, String kid, String use) { |
53 | 55 |
|
54 | | - ECPoint publicKeyPoint = privateKey.getPubKeyPoint(); |
55 | | - byte[] privateKeyBytes = privateKey.getPrivKeyBytes(); |
56 | | - Base64URL xParameter = Base64URL.encode(publicKeyPoint.getAffineXCoord().getEncoded()); |
57 | | - Base64URL yParameter = Base64URL.encode(publicKeyPoint.getAffineYCoord().getEncoded()); |
58 | | - Base64URL dParameter = Base64URL.encode(privateKeyBytes); |
| 56 | + byte[] publicKeyBytes = privateKey.publicKey; |
| 57 | + byte[] privateKeyBytes = privateKey.secretKey; |
59 | 58 |
|
60 | | - com.nimbusds.jose.jwk.ECKey jsonWebKey = new com.nimbusds.jose.jwk.ECKey.Builder(Curves.BLS12381_G1, xParameter, yParameter) |
61 | | - .d(dParameter) |
62 | | - .keyID(kid) |
63 | | - .keyUse(use == null ? null : new KeyUse(use)) |
64 | | - .build(); |
| 59 | + JWK jsonWebKey = new JWK(); |
| 60 | + jsonWebKey.setKty(KeyType.EC); |
| 61 | + jsonWebKey.setCrv(Curve.BLS12381_G1); |
| 62 | + jsonWebKey.setKid(kid); |
| 63 | + jsonWebKey.setUse(use); |
| 64 | + jsonWebKey.setX(Base64.encodeBase64URLSafeString(publicKeyBytes)); |
| 65 | + jsonWebKey.setD(Base64.encodeBase64URLSafeString(privateKeyBytes)); |
65 | 66 |
|
66 | 67 | return jsonWebKey; |
67 | 68 | } |
68 | 69 |
|
69 | | - public static com.nimbusds.jose.jwk.ECKey BLS12381_G1PrivateKeyBytes_to_JWK(byte[] privateKeyBytes, String kid, String use) { |
| 70 | + public static JWK BLS12381_G1PrivateKeyBytes_to_JWK(byte[] privateKeyBytes, byte[] publicKeyBytes, String kid, String use) { |
70 | 71 |
|
71 | | - ECKey privateKey = ECKey.fromPrivate(privateKeyBytes); |
| 72 | + KeyPair privateKey = new KeyPair(publicKeyBytes, privateKeyBytes); |
72 | 73 |
|
73 | | - return secp256k1PrivateKey_to_JWK(privateKey, kid, use); |
| 74 | + return BLS12381_G1PrivateKey_to_JWK(privateKey, kid, use); |
74 | 75 | } |
75 | 76 |
|
76 | | - public static com.nimbusds.jose.jwk.ECKey BLS12381_G2PrivateKey_to_JWK(ECKey privateKey, String kid, String use) { |
| 77 | + public static JWK BLS12381_G2PrivateKey_to_JWK(KeyPair privateKey, String kid, String use) { |
77 | 78 |
|
78 | | - ECPoint publicKeyPoint = privateKey.getPubKeyPoint(); |
79 | | - byte[] privateKeyBytes = privateKey.getPrivKeyBytes(); |
80 | | - Base64URL xParameter = Base64URL.encode(publicKeyPoint.getAffineXCoord().getEncoded()); |
81 | | - Base64URL yParameter = Base64URL.encode(publicKeyPoint.getAffineYCoord().getEncoded()); |
82 | | - Base64URL dParameter = Base64URL.encode(privateKeyBytes); |
| 79 | + byte[] publicKeyBytes = privateKey.publicKey; |
| 80 | + byte[] privateKeyBytes = privateKey.secretKey; |
83 | 81 |
|
84 | | - com.nimbusds.jose.jwk.ECKey jsonWebKey = new com.nimbusds.jose.jwk.ECKey.Builder(Curves.BLS12381_G2, xParameter, yParameter) |
85 | | - .d(dParameter) |
86 | | - .keyID(kid) |
87 | | - .keyUse(use == null ? null : new KeyUse(use)) |
88 | | - .build(); |
| 82 | + JWK jsonWebKey = new JWK(); |
| 83 | + jsonWebKey.setKty(KeyType.EC); |
| 84 | + jsonWebKey.setCrv(Curve.BLS12381_G2); |
| 85 | + jsonWebKey.setKid(kid); |
| 86 | + jsonWebKey.setUse(use); |
| 87 | + jsonWebKey.setX(Base64.encodeBase64URLSafeString(publicKeyBytes)); |
| 88 | + jsonWebKey.setD(Base64.encodeBase64URLSafeString(privateKeyBytes)); |
89 | 89 |
|
90 | 90 | return jsonWebKey; |
91 | 91 | } |
92 | 92 |
|
93 | | - public static com.nimbusds.jose.jwk.ECKey BLS12381_G2PrivateKeyBytes_to_JWK(byte[] privateKeyBytes, String kid, String use) { |
| 93 | + public static JWK BLS12381_G2PrivateKeyBytes_to_JWK(byte[] privateKeyBytes, byte[] publicKeyBytes, String kid, String use) { |
94 | 94 |
|
95 | | - ECKey privateKey = ECKey.fromPrivate(privateKeyBytes); |
| 95 | + KeyPair privateKey = new KeyPair(publicKeyBytes, privateKeyBytes); |
96 | 96 |
|
97 | | - return secp256k1PrivateKey_to_JWK(privateKey, kid, use); |
| 97 | + return BLS12381_G2PrivateKey_to_JWK(privateKey, kid, use); |
98 | 98 | } |
99 | 99 |
|
100 | | - public static com.nimbusds.jose.jwk.OctetKeyPair Ed25519PrivateKeyBytes_to_JWK(byte[] privateKeyBytes, byte[] publicKeyBytes, String kid, String use) { |
| 100 | + public static JWK Ed25519PrivateKeyBytes_to_JWK(byte[] privateKeyBytes, byte[] publicKeyBytes, String kid, String use) { |
101 | 101 |
|
102 | 102 | byte[] onlyPrivateKeyBytes = Arrays.copyOf(privateKeyBytes, 32); |
103 | | - Base64URL xParameter = Base64URL.encode(publicKeyBytes); |
104 | | - Base64URL dParameter = Base64URL.encode(onlyPrivateKeyBytes); |
105 | 103 |
|
106 | | - com.nimbusds.jose.jwk.OctetKeyPair jsonWebKey = new com.nimbusds.jose.jwk.OctetKeyPair.Builder(Curve.Ed25519, xParameter) |
107 | | - .d(dParameter) |
108 | | - .keyID(kid) |
109 | | - .keyUse(use == null ? null : new KeyUse(use)) |
110 | | - .build(); |
| 104 | + JWK jsonWebKey = new JWK(); |
| 105 | + jsonWebKey.setKty(KeyType.OKP); |
| 106 | + jsonWebKey.setCrv(Curve.Ed25519); |
| 107 | + jsonWebKey.setKid(kid); |
| 108 | + jsonWebKey.setUse(use); |
| 109 | + jsonWebKey.setX(Base64.encodeBase64URLSafeString(publicKeyBytes)); |
| 110 | + jsonWebKey.setD(Base64.encodeBase64URLSafeString(onlyPrivateKeyBytes)); |
111 | 111 |
|
112 | 112 | return jsonWebKey; |
113 | 113 | } |
114 | 114 |
|
115 | | - public static com.nimbusds.jose.jwk.OctetKeyPair X25519PrivateKeyBytes_to_JWK(byte[] privateKeyBytes, byte[] publicKeyBytes, String kid, String use) { |
| 115 | + public static JWK X25519PrivateKeyBytes_to_JWK(byte[] privateKeyBytes, byte[] publicKeyBytes, String kid, String use) { |
116 | 116 |
|
117 | 117 | byte[] onlyPrivateKeyBytes = Arrays.copyOf(privateKeyBytes, 32); |
118 | | - Base64URL xParameter = Base64URL.encode(publicKeyBytes); |
119 | | - Base64URL dParameter = Base64URL.encode(onlyPrivateKeyBytes); |
120 | 118 |
|
121 | | - com.nimbusds.jose.jwk.OctetKeyPair jsonWebKey = new com.nimbusds.jose.jwk.OctetKeyPair.Builder(Curve.X25519, xParameter) |
122 | | - .d(dParameter) |
123 | | - .keyID(kid) |
124 | | - .keyUse(use == null ? null : new KeyUse(use)) |
125 | | - .build(); |
| 119 | + JWK jsonWebKey = new JWK(); |
| 120 | + jsonWebKey.setKty(KeyType.OKP); |
| 121 | + jsonWebKey.setCrv(Curve.X25519); |
| 122 | + jsonWebKey.setKid(kid); |
| 123 | + jsonWebKey.setUse(use); |
| 124 | + jsonWebKey.setX(Base64.encodeBase64URLSafeString(publicKeyBytes)); |
| 125 | + jsonWebKey.setD(Base64.encodeBase64URLSafeString(onlyPrivateKeyBytes)); |
126 | 126 |
|
127 | 127 | return jsonWebKey; |
128 | 128 | } |
|
0 commit comments