2020import io .jsonwebtoken .lang .Classes ;
2121import io .jsonwebtoken .lang .Registry ;
2222import io .jsonwebtoken .security .AeadAlgorithm ;
23+ import io .jsonwebtoken .security .AeadRequest ;
24+ import io .jsonwebtoken .security .AeadResult ;
25+ import io .jsonwebtoken .security .DecryptAeadRequest ;
26+ import io .jsonwebtoken .security .DecryptionKeyRequest ;
2327import io .jsonwebtoken .security .KeyAlgorithm ;
2428import io .jsonwebtoken .security .KeyPairBuilderSupplier ;
29+ import io .jsonwebtoken .security .KeyRequest ;
2530import io .jsonwebtoken .security .MacAlgorithm ;
2631import io .jsonwebtoken .security .Password ;
32+ import io .jsonwebtoken .security .Request ;
2733import io .jsonwebtoken .security .SecretKeyAlgorithm ;
2834import io .jsonwebtoken .security .SecureDigestAlgorithm ;
35+ import io .jsonwebtoken .security .SecureRequest ;
2936import io .jsonwebtoken .security .SignatureAlgorithm ;
37+ import io .jsonwebtoken .security .VerifyDigestRequest ;
38+ import io .jsonwebtoken .security .VerifySecureDigestRequest ;
3039import io .jsonwebtoken .security .X509Builder ;
3140
3241import javax .crypto .SecretKey ;
42+ import java .io .InputStream ;
43+ import java .io .OutputStream ;
3344import java .security .Key ;
3445import java .security .PrivateKey ;
3546import java .security .PublicKey ;
@@ -82,6 +93,18 @@ public static final class ENC {
8293 private static final String IMPL_CLASSNAME = "io.jsonwebtoken.impl.security.StandardEncryptionAlgorithms" ;
8394 private static final Registry <String , AeadAlgorithm > REGISTRY = Classes .newInstance (IMPL_CLASSNAME );
8495
96+ // @since 0.13.0
97+ private static final Supplier <AeadRequest .Builder > REQUEST_BUILDER_SUPPLIER =
98+ Classes .newInstance ("io.jsonwebtoken.impl.security.DefaultAeadRequest$Builder$Supplier" );
99+
100+ // @since 0.13.0
101+ private static final Supplier <DecryptAeadRequest .Builder > DECRYPT_REQUEST_BUILDER_SUPPLIER =
102+ Classes .newInstance ("io.jsonwebtoken.impl.security.DefaultDecryptAeadRequest$Builder$Supplier" );
103+
104+ // @since 0.13.0
105+ private static final Supplier <AeadResult .Builder > RESULT_BUILDER_SUPPLIER =
106+ Classes .newInstance ("io.jsonwebtoken.impl.security.DefaultAeadResult$Builder$Supplier" );
107+
85108 /**
86109 * Returns all standard JWA <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-5">Cryptographic
87110 * Algorithms for Content Encryption</a> defined in the
@@ -139,6 +162,42 @@ private ENC() {
139162 * algorithm requires a 256-bit (32 byte) key.
140163 */
141164 public static final AeadAlgorithm A256GCM = get ().forKey ("A256GCM" );
165+
166+ /**
167+ * Returns a new builder to create {@link AeadRequest}s used for AEAD encryption via
168+ * {@link AeadAlgorithm#encrypt(AeadRequest, AeadResult)}
169+ *
170+ * @return a new builder to create {@link AeadRequest}s used for AEAD encryption via
171+ * {@link AeadAlgorithm#encrypt(AeadRequest, AeadResult)}
172+ * @since 0.13.0
173+ */
174+ public static AeadRequest .Builder request () {
175+ return REQUEST_BUILDER_SUPPLIER .get ();
176+ }
177+
178+ /**
179+ * Returns a new builder to create {@link DecryptAeadRequest}s used for AEAD decryption via
180+ * {@link AeadAlgorithm#decrypt(DecryptAeadRequest, OutputStream)}
181+ *
182+ * @return a new builder to create {@link DecryptAeadRequest}s used for AEAD decryption via
183+ * {@link AeadAlgorithm#decrypt(DecryptAeadRequest, OutputStream)}
184+ * @since 0.13.0
185+ */
186+ public static DecryptAeadRequest .Builder decryptRequest () {
187+ return DECRYPT_REQUEST_BUILDER_SUPPLIER .get ();
188+ }
189+
190+ /**
191+ * Returns a new builder to create {@link AeadResult}s used to store AEAD encryption results when calling
192+ * {@link AeadAlgorithm#encrypt(AeadRequest, AeadResult)}
193+ *
194+ * @return a new builder to create {@link AeadResult}s used to store AEAD encryption results when calling
195+ * {@link AeadAlgorithm#encrypt(AeadRequest, AeadResult)}
196+ * @since 0.13.0
197+ */
198+ public static AeadResult .Builder result () {
199+ return RESULT_BUILDER_SUPPLIER .get ();
200+ }
142201 }
143202
144203 /**
@@ -162,6 +221,14 @@ public static final class SIG {
162221 private static final String IMPL_CLASSNAME = "io.jsonwebtoken.impl.security.StandardSecureDigestAlgorithms" ;
163222 private static final Registry <String , SecureDigestAlgorithm <?, ?>> REGISTRY = Classes .newInstance (IMPL_CLASSNAME );
164223
224+ // @since 0.13.0
225+ private static final Supplier <SecureRequest .Builder <InputStream , ?>> REQUEST_BUILDER_SUPPLIER =
226+ Classes .newInstance ("io.jsonwebtoken.impl.security.DefaultSecureRequest$Builder$Supplier" );
227+
228+ // @since 0.13.0
229+ private static final Supplier <VerifySecureDigestRequest .Builder <?>> VERIFY_REQUEST_BUILDER_SUPPLIER =
230+ Classes .newInstance ("io.jsonwebtoken.impl.security.DefaultVerifySecureDigestRequest$Builder$Supplier" );
231+
165232 //prevent instantiation
166233 private SIG () {
167234 }
@@ -302,6 +369,34 @@ private SIG() {
302369 * classpath.</b></p>
303370 */
304371 public static final SignatureAlgorithm EdDSA = Jwts .get (REGISTRY , "EdDSA" );
372+
373+ /**
374+ * Returns a new builder to create {@link SecureRequest}s used to compute a mac or signature via
375+ * {@link SecureDigestAlgorithm#digest(Request)}.
376+ *
377+ * @param <K> the type of key used by the algorithm to compute the mac or signature.
378+ * @return a new builder to create {@link SecureRequest}s used to compute a mac or signature via
379+ * {@link SecureDigestAlgorithm#digest(Request)}.
380+ * @since 0.13.0
381+ */
382+ @ SuppressWarnings ("unchecked" )
383+ public static <K extends Key > SecureRequest .Builder <InputStream , K > request () {
384+ return (SecureRequest .Builder <InputStream , K >) REQUEST_BUILDER_SUPPLIER .get ();
385+ }
386+
387+ /**
388+ * Returns a new builder to create {@link VerifySecureDigestRequest}s used to verify a mac or signature via
389+ * {@link SecureDigestAlgorithm#verify(VerifyDigestRequest)}.
390+ *
391+ * @param <K> the type of key used by the algorithm to verify the mac or signature.
392+ * @return a new builder to create {@link VerifySecureDigestRequest}s used to verify a mac or signature via
393+ * {@link SecureDigestAlgorithm#verify(VerifyDigestRequest)}.
394+ * @since 0.13.0
395+ */
396+ @ SuppressWarnings ("unchecked" )
397+ public static <K extends Key > VerifySecureDigestRequest .Builder <K > verifyRequest () {
398+ return (VerifySecureDigestRequest .Builder <K >) VERIFY_REQUEST_BUILDER_SUPPLIER .get ();
399+ }
305400 }
306401
307402 /**
@@ -323,6 +418,14 @@ public static final class KEY {
323418 private static final String IMPL_CLASSNAME = "io.jsonwebtoken.impl.security.StandardKeyAlgorithms" ;
324419 private static final Registry <String , KeyAlgorithm <?, ?>> REGISTRY = Classes .newInstance (IMPL_CLASSNAME );
325420
421+ // @since 0.13.0
422+ private static final Supplier <KeyRequest .Builder <?>> REQUEST_BUILDER_SUPPLIER =
423+ Classes .newInstance ("io.jsonwebtoken.impl.security.DefaultKeyRequest$Builder$Supplier" );
424+
425+ // @since 0.13.0
426+ private static final Supplier <DecryptionKeyRequest .Builder <?>> VERIFY_REQUEST_BUILDER_SUPPLIER =
427+ Classes .newInstance ("io.jsonwebtoken.impl.security.DefaultDecryptionKeyRequest$Builder$Supplier" );
428+
326429 /**
327430 * Returns all standard JWA standard <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-4">Cryptographic
328431 * Algorithms for Key Management</a>..
@@ -926,6 +1029,34 @@ public static final class KEY {
9261029 */
9271030 public static final KeyAlgorithm <PublicKey , PrivateKey > ECDH_ES_A256KW = Jwts .get (REGISTRY , "ECDH-ES+A256KW" );
9281031
1032+ /**
1033+ * Returns a new builder to create {@link KeyRequest}s used to get a JWE encryption key via
1034+ * {@link KeyAlgorithm#getEncryptionKey(KeyRequest)}.
1035+ *
1036+ * @param <K> the type of key used by the {@link KeyAlgorithm} to get a JWE encryption key.
1037+ * @return a new builder to create {@link KeyRequest}s used to get a JWE encryption key via
1038+ * {@link KeyAlgorithm#getEncryptionKey(KeyRequest)}.
1039+ * @since 0.13.0
1040+ */
1041+ @ SuppressWarnings ("unchecked" )
1042+ public static <K extends Key > KeyRequest .Builder <K > request () {
1043+ return (KeyRequest .Builder <K >) REQUEST_BUILDER_SUPPLIER .get ();
1044+ }
1045+
1046+ /**
1047+ * Returns a new builder to create {@link DecryptionKeyRequest}s used to get a JWE decryption key via
1048+ * {@link KeyAlgorithm#getDecryptionKey(DecryptionKeyRequest)}.
1049+ *
1050+ * @param <K> the type of key used by the {@link KeyAlgorithm} to get a JWE decryption key.
1051+ * @return a new builder to create {@link DecryptionKeyRequest}s used to get a JWE decryption key via
1052+ * {@link KeyAlgorithm#getDecryptionKey(DecryptionKeyRequest)}.
1053+ * @since 0.13.0
1054+ */
1055+ @ SuppressWarnings ("unchecked" )
1056+ public static <K extends Key > DecryptionKeyRequest .Builder <K > decRequest () {
1057+ return (DecryptionKeyRequest .Builder <K >) VERIFY_REQUEST_BUILDER_SUPPLIER .get ();
1058+ }
1059+
9291060 //prevent instantiation
9301061 private KEY () {
9311062 }
0 commit comments