44import com .auth0 .jwt .exceptions .SignatureVerificationException ;
55
66import java .io .UnsupportedEncodingException ;
7- import java .security .interfaces .ECKey ;
8- import java .security .interfaces .RSAKey ;
7+ import java .security .PrivateKey ;
8+ import java .security .PublicKey ;
9+ import java .security .interfaces .*;
910
1011/**
1112 * The Algorithm class represents an algorithm to be used in the Signing or Verification process of a Token.
1213 */
14+ @ SuppressWarnings ("WeakerAccess" )
1315public abstract class Algorithm {
1416
1517 private final String name ;
@@ -21,9 +23,13 @@ public abstract class Algorithm {
2123 * @param key the key to use in the verify or signing instance.
2224 * @return a valid RSA256 Algorithm.
2325 * @throws IllegalArgumentException if the provided Key is null.
26+ * @deprecated use {@link #RSA256(RSAPublicKey, RSAPrivateKey)}
2427 */
28+ @ Deprecated
2529 public static Algorithm RSA256 (RSAKey key ) throws IllegalArgumentException {
26- return new RSAAlgorithm ("RS256" , "SHA256withRSA" , key );
30+ RSAPublicKey publicKey = key instanceof PublicKey ? (RSAPublicKey ) key : null ;
31+ RSAPrivateKey privateKey = key instanceof PrivateKey ? (RSAPrivateKey ) key : null ;
32+ return RSA256 (publicKey , privateKey );
2733 }
2834
2935 /**
@@ -32,9 +38,13 @@ public static Algorithm RSA256(RSAKey key) throws IllegalArgumentException {
3238 * @param key the key to use in the verify or signing instance.
3339 * @return a valid RSA384 Algorithm.
3440 * @throws IllegalArgumentException if the provided Key is null.
41+ * @deprecated use {@link #RSA384(RSAPublicKey, RSAPrivateKey)}
3542 */
43+ @ Deprecated
3644 public static Algorithm RSA384 (RSAKey key ) throws IllegalArgumentException {
37- return new RSAAlgorithm ("RS384" , "SHA384withRSA" , key );
45+ RSAPublicKey publicKey = key instanceof PublicKey ? (RSAPublicKey ) key : null ;
46+ RSAPrivateKey privateKey = key instanceof PrivateKey ? (RSAPrivateKey ) key : null ;
47+ return RSA384 (publicKey , privateKey );
3848 }
3949
4050 /**
@@ -43,9 +53,49 @@ public static Algorithm RSA384(RSAKey key) throws IllegalArgumentException {
4353 * @param key the key to use in the verify or signing instance.
4454 * @return a valid RSA512 Algorithm.
4555 * @throws IllegalArgumentException if the provided Key is null.
56+ * @deprecated use {@link #RSA512(RSAPublicKey, RSAPrivateKey)}
4657 */
58+ @ Deprecated
4759 public static Algorithm RSA512 (RSAKey key ) throws IllegalArgumentException {
48- return new RSAAlgorithm ("RS512" , "SHA512withRSA" , key );
60+ RSAPublicKey publicKey = key instanceof PublicKey ? (RSAPublicKey ) key : null ;
61+ RSAPrivateKey privateKey = key instanceof PrivateKey ? (RSAPrivateKey ) key : null ;
62+ return RSA512 (publicKey , privateKey );
63+ }
64+
65+ /**
66+ * Creates a new Algorithm instance using SHA256withRSA. Tokens specify this as "RS256".
67+ *
68+ * @param publicKey the key to use in the verify instance.
69+ * @param privateKey the key to use in the signing instance.
70+ * @return a valid RSA256 Algorithm.
71+ * @throws IllegalArgumentException if both provided Keys are null.
72+ */
73+ public static Algorithm RSA256 (RSAPublicKey publicKey , RSAPrivateKey privateKey ) throws IllegalArgumentException {
74+ return new RSAAlgorithm ("RS256" , "SHA256withRSA" , publicKey , privateKey );
75+ }
76+
77+ /**
78+ * Creates a new Algorithm instance using SHA384withRSA. Tokens specify this as "RS384".
79+ *
80+ * @param publicKey the key to use in the verify instance.
81+ * @param privateKey the key to use in the signing instance.
82+ * @return a valid RSA384 Algorithm.
83+ * @throws IllegalArgumentException if both provided Keys are null.
84+ */
85+ public static Algorithm RSA384 (RSAPublicKey publicKey , RSAPrivateKey privateKey ) throws IllegalArgumentException {
86+ return new RSAAlgorithm ("RS384" , "SHA384withRSA" , publicKey , privateKey );
87+ }
88+
89+ /**
90+ * Creates a new Algorithm instance using SHA512withRSA. Tokens specify this as "RS512".
91+ *
92+ * @param publicKey the key to use in the verify instance.
93+ * @param privateKey the key to use in the signing instance.
94+ * @return a valid RSA512 Algorithm.
95+ * @throws IllegalArgumentException if both provided Keys are null.
96+ */
97+ public static Algorithm RSA512 (RSAPublicKey publicKey , RSAPrivateKey privateKey ) throws IllegalArgumentException {
98+ return new RSAAlgorithm ("RS512" , "SHA512withRSA" , publicKey , privateKey );
4999 }
50100
51101 /**
@@ -123,9 +173,13 @@ public static Algorithm HMAC512(byte[] secret) throws IllegalArgumentException {
123173 * @param key the key to use in the verify or signing instance.
124174 * @return a valid ECDSA256 Algorithm.
125175 * @throws IllegalArgumentException if the provided Key is null.
176+ * @deprecated use {@link #ECDSA256(ECPublicKey, ECPrivateKey)}
126177 */
178+ @ Deprecated
127179 public static Algorithm ECDSA256 (ECKey key ) throws IllegalArgumentException {
128- return new ECDSAAlgorithm ("ES256" , "SHA256withECDSA" , 32 , key );
180+ ECPublicKey publicKey = key instanceof PublicKey ? (ECPublicKey ) key : null ;
181+ ECPrivateKey privateKey = key instanceof PrivateKey ? (ECPrivateKey ) key : null ;
182+ return ECDSA256 (publicKey , privateKey );
129183 }
130184
131185 /**
@@ -134,9 +188,13 @@ public static Algorithm ECDSA256(ECKey key) throws IllegalArgumentException {
134188 * @param key the key to use in the verify or signing instance.
135189 * @return a valid ECDSA384 Algorithm.
136190 * @throws IllegalArgumentException if the provided Key is null.
191+ * @deprecated use {@link #ECDSA384(ECPublicKey, ECPrivateKey)}
137192 */
193+ @ Deprecated
138194 public static Algorithm ECDSA384 (ECKey key ) throws IllegalArgumentException {
139- return new ECDSAAlgorithm ("ES384" , "SHA384withECDSA" , 48 , key );
195+ ECPublicKey publicKey = key instanceof PublicKey ? (ECPublicKey ) key : null ;
196+ ECPrivateKey privateKey = key instanceof PrivateKey ? (ECPrivateKey ) key : null ;
197+ return ECDSA384 (publicKey , privateKey );
140198 }
141199
142200 /**
@@ -145,9 +203,49 @@ public static Algorithm ECDSA384(ECKey key) throws IllegalArgumentException {
145203 * @param key the key to use in the verify or signing instance.
146204 * @return a valid ECDSA512 Algorithm.
147205 * @throws IllegalArgumentException if the provided Key is null.
206+ * @deprecated use {@link #ECDSA512(ECPublicKey, ECPrivateKey)}
148207 */
208+ @ Deprecated
149209 public static Algorithm ECDSA512 (ECKey key ) throws IllegalArgumentException {
150- return new ECDSAAlgorithm ("ES512" , "SHA512withECDSA" , 66 , key );
210+ ECPublicKey publicKey = key instanceof PublicKey ? (ECPublicKey ) key : null ;
211+ ECPrivateKey privateKey = key instanceof PrivateKey ? (ECPrivateKey ) key : null ;
212+ return ECDSA512 (publicKey , privateKey );
213+ }
214+
215+ /**
216+ * Creates a new Algorithm instance using SHA256withECDSA. Tokens specify this as "ES256".
217+ *
218+ * @param publicKey the key to use in the verify instance.
219+ * @param privateKey the key to use in the signing instance.
220+ * @return a valid ECDSA256 Algorithm.
221+ * @throws IllegalArgumentException if the provided Key is null.
222+ */
223+ public static Algorithm ECDSA256 (ECPublicKey publicKey , ECPrivateKey privateKey ) throws IllegalArgumentException {
224+ return new ECDSAAlgorithm ("ES256" , "SHA256withECDSA" , 32 , publicKey , privateKey );
225+ }
226+
227+ /**
228+ * Creates a new Algorithm instance using SHA384withECDSA. Tokens specify this as "ES384".
229+ *
230+ * @param publicKey the key to use in the verify instance.
231+ * @param privateKey the key to use in the signing instance.
232+ * @return a valid ECDSA384 Algorithm.
233+ * @throws IllegalArgumentException if the provided Key is null.
234+ */
235+ public static Algorithm ECDSA384 (ECPublicKey publicKey , ECPrivateKey privateKey ) throws IllegalArgumentException {
236+ return new ECDSAAlgorithm ("ES384" , "SHA384withECDSA" , 48 , publicKey , privateKey );
237+ }
238+
239+ /**
240+ * Creates a new Algorithm instance using SHA512withECDSA. Tokens specify this as "ES512".
241+ *
242+ * @param publicKey the key to use in the verify instance.
243+ * @param privateKey the key to use in the signing instance.
244+ * @return a valid ECDSA512 Algorithm.
245+ * @throws IllegalArgumentException if the provided Key is null.
246+ */
247+ public static Algorithm ECDSA512 (ECPublicKey publicKey , ECPrivateKey privateKey ) throws IllegalArgumentException {
248+ return new ECDSAAlgorithm ("ES512" , "SHA512withECDSA" , 66 , publicKey , privateKey );
151249 }
152250
153251 public static Algorithm none () {
0 commit comments