|
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.security.GeneralSecurityException; |
5 | | -import java.security.PrivateKey; |
6 | 5 | import java.security.PublicKey; |
7 | 6 | import java.security.interfaces.RSAPublicKey; |
8 | 7 | import java.text.ParseException; |
|
15 | 14 | import com.nimbusds.jose.JWSHeader; |
16 | 15 | import com.nimbusds.jose.JWSSigner; |
17 | 16 | import com.nimbusds.jose.JWSVerifier; |
| 17 | +import com.nimbusds.jose.crypto.ECDSASigner; |
| 18 | +import com.nimbusds.jose.crypto.Ed25519Signer; |
18 | 19 | import com.nimbusds.jose.crypto.RSASSASigner; |
19 | 20 | import com.nimbusds.jose.crypto.RSASSAVerifier; |
| 21 | +import com.nimbusds.jose.jwk.ECKey; |
| 22 | +import com.nimbusds.jose.jwk.OctetKeyPair; |
| 23 | +import com.nimbusds.jose.jwk.RSAKey; |
20 | 24 | import com.nimbusds.jwt.JWTClaimsSet; |
21 | 25 | import com.nimbusds.jwt.SignedJWT; |
22 | 26 |
|
@@ -141,12 +145,38 @@ public String getCompactSerialization() { |
141 | 145 | return this.compactSerialization; |
142 | 146 | } |
143 | 147 |
|
144 | | - public String toJwt(String algorithm, PrivateKey privateKey) throws JOSEException { |
| 148 | + public String toJwtRSA(RSAKey rsaKey) throws JOSEException { |
145 | 149 |
|
146 | | - JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.parse(algorithm)).build(); |
| 150 | + JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256).build(); |
147 | 151 | SignedJWT signedJWT = new SignedJWT(jwsHeader, this.getPayload()); |
148 | 152 |
|
149 | | - JWSSigner signer = new RSASSASigner(privateKey); |
| 153 | + JWSSigner signer = new RSASSASigner(rsaKey); |
| 154 | + |
| 155 | + signedJWT.sign(signer); |
| 156 | + |
| 157 | + this.compactSerialization = signedJWT.serialize(); |
| 158 | + return compactSerialization; |
| 159 | + } |
| 160 | + |
| 161 | + public String toJwtEd25519(OctetKeyPair octetKeyPair) throws JOSEException { |
| 162 | + |
| 163 | + JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.EdDSA).build(); |
| 164 | + SignedJWT signedJWT = new SignedJWT(jwsHeader, this.getPayload()); |
| 165 | + |
| 166 | + JWSSigner signer = new Ed25519Signer(octetKeyPair); |
| 167 | + |
| 168 | + signedJWT.sign(signer); |
| 169 | + |
| 170 | + this.compactSerialization = signedJWT.serialize(); |
| 171 | + return compactSerialization; |
| 172 | + } |
| 173 | + |
| 174 | + public String toJwtES256K(ECKey ecKey) throws JOSEException { |
| 175 | + |
| 176 | + JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.ES256K).build(); |
| 177 | + SignedJWT signedJWT = new SignedJWT(jwsHeader, this.getPayload()); |
| 178 | + |
| 179 | + JWSSigner signer = new ECDSASigner(ecKey); |
150 | 180 |
|
151 | 181 | signedJWT.sign(signer); |
152 | 182 |
|
|
0 commit comments