11package com .auth0 .jwt .algorithms ;
22
3+ import com .auth0 .jwt .exceptions .SignatureGenerationException ;
34import com .auth0 .jwt .exceptions .SignatureVerificationException ;
45
5- import java .security .PublicKey ;
6+ import java .security .interfaces .ECKey ;
7+ import java .security .interfaces .RSAKey ;
68
79/**
810 * The Algorithm class represents an algorithm to be used in the Signing or Verification process of a Token.
@@ -15,37 +17,37 @@ public abstract class Algorithm {
1517 /**
1618 * Creates a new Algorithm instance using SHA256withRSA. Tokens specify this as "RS256".
1719 *
18- * @param publicKey the key to use in the verify instance.
20+ * @param key the key to use in the verify or signing instance.
1921 * @return a valid RSA256 Algorithm.
2022 */
21- public static Algorithm RSA256 (PublicKey publicKey ) {
22- return new RSAAlgorithm ("RS256" , "SHA256withRSA" , publicKey );
23+ public static Algorithm RSA256 (RSAKey key ) {
24+ return new RSAAlgorithm ("RS256" , "SHA256withRSA" , key );
2325 }
2426
2527 /**
2628 * Creates a new Algorithm instance using SHA384withRSA. Tokens specify this as "RS384".
2729 *
28- * @param publicKey the key to use in the verify instance.
30+ * @param key the key to use in the verify or signing instance.
2931 * @return a valid RSA384 Algorithm.
3032 */
31- public static Algorithm RSA384 (PublicKey publicKey ) {
32- return new RSAAlgorithm ("RS384" , "SHA384withRSA" , publicKey );
33+ public static Algorithm RSA384 (RSAKey key ) {
34+ return new RSAAlgorithm ("RS384" , "SHA384withRSA" , key );
3335 }
3436
3537 /**
3638 * Creates a new Algorithm instance using SHA512withRSA. Tokens specify this as "RS512".
3739 *
38- * @param publicKey the key to use in the verify instance.
40+ * @param key the key to use in the verify or signing instance.
3941 * @return a valid RSA512 Algorithm.
4042 */
41- public static Algorithm RSA512 (PublicKey publicKey ) {
42- return new RSAAlgorithm ("RS512" , "SHA512withRSA" , publicKey );
43+ public static Algorithm RSA512 (RSAKey key ) {
44+ return new RSAAlgorithm ("RS512" , "SHA512withRSA" , key );
4345 }
4446
4547 /**
4648 * Creates a new Algorithm instance using HmacSHA256. Tokens specify this as "HS256".
4749 *
48- * @param secret the secret to use in the verify instance.
50+ * @param secret the secret to use in the verify or signing instance.
4951 * @return a valid HMAC256 Algorithm.
5052 */
5153 public static Algorithm HMAC256 (String secret ) {
@@ -55,7 +57,7 @@ public static Algorithm HMAC256(String secret) {
5557 /**
5658 * Creates a new Algorithm instance using HmacSHA384. Tokens specify this as "HS384".
5759 *
58- * @param secret the secret to use in the verify instance.
60+ * @param secret the secret to use in the verify or signing instance.
5961 * @return a valid HMAC384 Algorithm.
6062 */
6163 public static Algorithm HMAC384 (String secret ) {
@@ -65,7 +67,7 @@ public static Algorithm HMAC384(String secret) {
6567 /**
6668 * Creates a new Algorithm instance using HmacSHA512. Tokens specify this as "HS512".
6769 *
68- * @param secret the secret to use in the verify instance.
70+ * @param secret the secret to use in the verify or signing instance.
6971 * @return a valid HMAC512 Algorithm.
7072 */
7173 public static Algorithm HMAC512 (String secret ) {
@@ -75,31 +77,31 @@ public static Algorithm HMAC512(String secret) {
7577 /**
7678 * Creates a new Algorithm instance using SHA256withECDSA. Tokens specify this as "ES256".
7779 *
78- * @param publicKey the key to use in the verify instance.
80+ * @param key the key to use in the verify or signing instance.
7981 * @return a valid ECDSA256 Algorithm.
8082 */
81- public static Algorithm ECDSA256 (PublicKey publicKey ) {
82- return new ECDSAAlgorithm ("ES256" , "SHA256withECDSA" , 32 , publicKey );
83+ public static Algorithm ECDSA256 (ECKey key ) {
84+ return new ECDSAAlgorithm ("ES256" , "SHA256withECDSA" , 32 , key );
8385 }
8486
8587 /**
8688 * Creates a new Algorithm instance using SHA384withECDSA. Tokens specify this as "ES384".
8789 *
88- * @param publicKey the key to use in the verify instance.
90+ * @param key the key to use in the verify or signing instance.
8991 * @return a valid ECDSA384 Algorithm.
9092 */
91- public static Algorithm ECDSA384 (PublicKey publicKey ) {
92- return new ECDSAAlgorithm ("ES384" , "SHA384withECDSA" , 48 , publicKey );
93+ public static Algorithm ECDSA384 (ECKey key ) {
94+ return new ECDSAAlgorithm ("ES384" , "SHA384withECDSA" , 48 , key );
9395 }
9496
9597 /**
9698 * Creates a new Algorithm instance using SHA512withECDSA. Tokens specify this as "ES512".
9799 *
98- * @param publicKey the key to use in the verify instance.
100+ * @param key the key to use in the verify or signing instance.
99101 * @return a valid ECDSA512 Algorithm.
100102 */
101- public static Algorithm ECDSA512 (PublicKey publicKey ) {
102- return new ECDSAAlgorithm ("ES512" , "SHA512withECDSA" , 66 , publicKey );
103+ public static Algorithm ECDSA512 (ECKey key ) {
104+ return new ECDSAAlgorithm ("ES512" , "SHA512withECDSA" , 66 , key );
103105 }
104106
105107 public static Algorithm none () {
@@ -141,4 +143,6 @@ public String toString() {
141143 * @throws SignatureVerificationException if the Token's Signature is invalid.
142144 */
143145 public abstract void verify (String [] jwtParts ) throws SignatureVerificationException ;
146+
147+ public abstract byte [] sign (byte [] headerAndPayloadBytes ) throws SignatureGenerationException ;
144148}
0 commit comments