Package com.auth0.jwt.algorithms
Class Algorithm
java.lang.Object
com.auth0.jwt.algorithms.Algorithm
The Algorithm class represents an algorithm to be used in the Signing or Verification process of a Token.
This class and its subclasses are thread-safe.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic AlgorithmECDSA256(ECDSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA256withECDSA.static AlgorithmDeprecated.static AlgorithmECDSA256(ECPublicKey publicKey, ECPrivateKey privateKey)Creates a new Algorithm instance using SHA256withECDSA.static AlgorithmECDSA256K(ECDSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA256withECDSA.static AlgorithmECDSA256K(ECPublicKey publicKey, ECPrivateKey privateKey)Creates a new Algorithm instance using SHA256withECDSA.static AlgorithmECDSA384(ECDSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA384withECDSA.static AlgorithmDeprecated.static AlgorithmECDSA384(ECPublicKey publicKey, ECPrivateKey privateKey)Creates a new Algorithm instance using SHA384withECDSA.static AlgorithmECDSA512(ECDSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA512withECDSA.static AlgorithmDeprecated.static AlgorithmECDSA512(ECPublicKey publicKey, ECPrivateKey privateKey)Creates a new Algorithm instance using SHA512withECDSA.getName()Getter for the name of this Algorithm, as defined in the JWT Standard.Getter for the Id of the Private Key used to sign the tokens.static AlgorithmHMAC256(byte[] secret)Creates a new Algorithm instance using HmacSHA256.static AlgorithmCreates a new Algorithm instance using HmacSHA256.static AlgorithmHMAC384(byte[] secret)Creates a new Algorithm instance using HmacSHA384.static AlgorithmCreates a new Algorithm instance using HmacSHA384.static AlgorithmHMAC512(byte[] secret)Creates a new Algorithm instance using HmacSHA512.static AlgorithmCreates a new Algorithm instance using HmacSHA512.static Algorithmnone()static AlgorithmRSA256(RSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA256withRSA.static AlgorithmDeprecated.static AlgorithmRSA256(RSAPublicKey publicKey, RSAPrivateKey privateKey)Creates a new Algorithm instance using SHA256withRSA.static AlgorithmRSA384(RSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA384withRSA.static AlgorithmDeprecated.static AlgorithmRSA384(RSAPublicKey publicKey, RSAPrivateKey privateKey)Creates a new Algorithm instance using SHA384withRSA.static AlgorithmRSA512(RSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA512withRSA.static AlgorithmDeprecated.static AlgorithmRSA512(RSAPublicKey publicKey, RSAPrivateKey privateKey)Creates a new Algorithm instance using SHA512withRSA.abstract byte[]sign(byte[] contentBytes)Deprecated.Please use the sign(byte[], byte[]) method instead.byte[]sign(byte[] headerBytes, byte[] payloadBytes)Sign the given content using this Algorithm instance.toString()abstract voidverify(DecodedJWT jwt)Verify the given token using this Algorithm instance.
-
Constructor Details
-
Algorithm
-
-
Method Details
-
RSA256
Creates a new Algorithm instance using SHA256withRSA. Tokens specify this as "RS256".- Parameters:
keyProvider- the provider of the Public Key and Private Key for the verify and signing instance.- Returns:
- a valid RSA256 Algorithm.
- Throws:
IllegalArgumentException- if the provided Key is null.
-
RSA256
public static Algorithm RSA256(RSAPublicKey publicKey, RSAPrivateKey privateKey) throws IllegalArgumentExceptionCreates a new Algorithm instance using SHA256withRSA. Tokens specify this as "RS256".- Parameters:
publicKey- the key to use in the verify instance.privateKey- the key to use in the signing instance.- Returns:
- a valid RSA256 Algorithm.
- Throws:
IllegalArgumentException- if both provided Keys are null.
-
RSA256
Deprecated.Creates a new Algorithm instance using SHA256withRSA. Tokens specify this as "RS256".- Parameters:
key- the key to use in the verify or signing instance.- Returns:
- a valid RSA256 Algorithm.
- Throws:
IllegalArgumentException- if the Key Provider is null.
-
RSA384
Creates a new Algorithm instance using SHA384withRSA. Tokens specify this as "RS384".- Parameters:
keyProvider- the provider of the Public Key and Private Key for the verify and signing instance.- Returns:
- a valid RSA384 Algorithm.
- Throws:
IllegalArgumentException- if the Key Provider is null.
-
RSA384
public static Algorithm RSA384(RSAPublicKey publicKey, RSAPrivateKey privateKey) throws IllegalArgumentExceptionCreates a new Algorithm instance using SHA384withRSA. Tokens specify this as "RS384".- Parameters:
publicKey- the key to use in the verify instance.privateKey- the key to use in the signing instance.- Returns:
- a valid RSA384 Algorithm.
- Throws:
IllegalArgumentException- if both provided Keys are null.
-
RSA384
Deprecated.Creates a new Algorithm instance using SHA384withRSA. Tokens specify this as "RS384".- Parameters:
key- the key to use in the verify or signing instance.- Returns:
- a valid RSA384 Algorithm.
- Throws:
IllegalArgumentException- if the provided Key is null.
-
RSA512
Creates a new Algorithm instance using SHA512withRSA. Tokens specify this as "RS512".- Parameters:
keyProvider- the provider of the Public Key and Private Key for the verify and signing instance.- Returns:
- a valid RSA512 Algorithm.
- Throws:
IllegalArgumentException- if the Key Provider is null.
-
RSA512
public static Algorithm RSA512(RSAPublicKey publicKey, RSAPrivateKey privateKey) throws IllegalArgumentExceptionCreates a new Algorithm instance using SHA512withRSA. Tokens specify this as "RS512".- Parameters:
publicKey- the key to use in the verify instance.privateKey- the key to use in the signing instance.- Returns:
- a valid RSA512 Algorithm.
- Throws:
IllegalArgumentException- if both provided Keys are null.
-
RSA512
Deprecated.Creates a new Algorithm instance using SHA512withRSA. Tokens specify this as "RS512".- Parameters:
key- the key to use in the verify or signing instance.- Returns:
- a valid RSA512 Algorithm.
- Throws:
IllegalArgumentException- if the provided Key is null.
-
HMAC256
Creates a new Algorithm instance using HmacSHA256. Tokens specify this as "HS256".- Parameters:
secret- the secret to use in the verify or signing instance.- Returns:
- a valid HMAC256 Algorithm.
- Throws:
IllegalArgumentException- if the provided Secret is null.
-
HMAC384
Creates a new Algorithm instance using HmacSHA384. Tokens specify this as "HS384".- Parameters:
secret- the secret to use in the verify or signing instance.- Returns:
- a valid HMAC384 Algorithm.
- Throws:
IllegalArgumentException- if the provided Secret is null.
-
HMAC512
Creates a new Algorithm instance using HmacSHA512. Tokens specify this as "HS512".- Parameters:
secret- the secret to use in the verify or signing instance.- Returns:
- a valid HMAC512 Algorithm.
- Throws:
IllegalArgumentException- if the provided Secret is null.
-
HMAC256
Creates a new Algorithm instance using HmacSHA256. Tokens specify this as "HS256".- Parameters:
secret- the secret bytes to use in the verify or signing instance.- Returns:
- a valid HMAC256 Algorithm.
- Throws:
IllegalArgumentException- if the provided Secret is null.
-
ECDSA256K
Creates a new Algorithm instance using SHA256withECDSA. Tokens specify this as "ES256K".- Parameters:
keyProvider- the provider of the Public Key and Private Key for the verify and signing instance.- Returns:
- a valid ECDSA256 Algorithm.
- Throws:
IllegalArgumentException- if the Key Provider is null.
-
ECDSA256K
public static Algorithm ECDSA256K(ECPublicKey publicKey, ECPrivateKey privateKey) throws IllegalArgumentExceptionCreates a new Algorithm instance using SHA256withECDSA. Tokens specify this as "ES256K".- Parameters:
publicKey- the key to use in the verify instance.privateKey- the key to use in the signing instance.- Returns:
- a valid ECDSA256 Algorithm.
- Throws:
IllegalArgumentException- if the provided Key is null.
-
HMAC384
Creates a new Algorithm instance using HmacSHA384. Tokens specify this as "HS384".- Parameters:
secret- the secret bytes to use in the verify or signing instance.- Returns:
- a valid HMAC384 Algorithm.
- Throws:
IllegalArgumentException- if the provided Secret is null.
-
HMAC512
Creates a new Algorithm instance using HmacSHA512. Tokens specify this as "HS512".- Parameters:
secret- the secret bytes to use in the verify or signing instance.- Returns:
- a valid HMAC512 Algorithm.
- Throws:
IllegalArgumentException- if the provided Secret is null.
-
ECDSA256
Creates a new Algorithm instance using SHA256withECDSA. Tokens specify this as "ES256".- Parameters:
keyProvider- the provider of the Public Key and Private Key for the verify and signing instance.- Returns:
- a valid ECDSA256 Algorithm.
- Throws:
IllegalArgumentException- if the Key Provider is null.
-
ECDSA256
public static Algorithm ECDSA256(ECPublicKey publicKey, ECPrivateKey privateKey) throws IllegalArgumentExceptionCreates a new Algorithm instance using SHA256withECDSA. Tokens specify this as "ES256".- Parameters:
publicKey- the key to use in the verify instance.privateKey- the key to use in the signing instance.- Returns:
- a valid ECDSA256 Algorithm.
- Throws:
IllegalArgumentException- if the provided Key is null.
-
ECDSA256
Deprecated.Creates a new Algorithm instance using SHA256withECDSA. Tokens specify this as "ES256".- Parameters:
key- the key to use in the verify or signing instance.- Returns:
- a valid ECDSA256 Algorithm.
- Throws:
IllegalArgumentException- if the provided Key is null.
-
ECDSA384
Creates a new Algorithm instance using SHA384withECDSA. Tokens specify this as "ES384".- Parameters:
keyProvider- the provider of the Public Key and Private Key for the verify and signing instance.- Returns:
- a valid ECDSA384 Algorithm.
- Throws:
IllegalArgumentException- if the Key Provider is null.
-
ECDSA384
public static Algorithm ECDSA384(ECPublicKey publicKey, ECPrivateKey privateKey) throws IllegalArgumentExceptionCreates a new Algorithm instance using SHA384withECDSA. Tokens specify this as "ES384".- Parameters:
publicKey- the key to use in the verify instance.privateKey- the key to use in the signing instance.- Returns:
- a valid ECDSA384 Algorithm.
- Throws:
IllegalArgumentException- if the provided Key is null.
-
ECDSA384
Deprecated.Creates a new Algorithm instance using SHA384withECDSA. Tokens specify this as "ES384".- Parameters:
key- the key to use in the verify or signing instance.- Returns:
- a valid ECDSA384 Algorithm.
- Throws:
IllegalArgumentException- if the provided Key is null.
-
ECDSA512
Creates a new Algorithm instance using SHA512withECDSA. Tokens specify this as "ES512".- Parameters:
keyProvider- the provider of the Public Key and Private Key for the verify and signing instance.- Returns:
- a valid ECDSA512 Algorithm.
- Throws:
IllegalArgumentException- if the Key Provider is null.
-
ECDSA512
public static Algorithm ECDSA512(ECPublicKey publicKey, ECPrivateKey privateKey) throws IllegalArgumentExceptionCreates a new Algorithm instance using SHA512withECDSA. Tokens specify this as "ES512".- Parameters:
publicKey- the key to use in the verify instance.privateKey- the key to use in the signing instance.- Returns:
- a valid ECDSA512 Algorithm.
- Throws:
IllegalArgumentException- if the provided Key is null.
-
ECDSA512
Deprecated.Creates a new Algorithm instance using SHA512withECDSA. Tokens specify this as "ES512".- Parameters:
key- the key to use in the verify or signing instance.- Returns:
- a valid ECDSA512 Algorithm.
- Throws:
IllegalArgumentException- if the provided Key is null.
-
none
-
getSigningKeyId
Getter for the Id of the Private Key used to sign the tokens. This is usually specified as the `kid` claim in the Header.- Returns:
- the Key Id that identifies the Signing Key or null if it's not specified.
-
getName
Getter for the name of this Algorithm, as defined in the JWT Standard. i.e. "HS256"- Returns:
- the algorithm name.
-
toString
-
verify
Verify the given token using this Algorithm instance.- Parameters:
jwt- the already decoded JWT that it's going to be verified.- Throws:
SignatureVerificationException- if the Token's Signature is invalid, meaning that it doesn't match the signatureBytes, or if the Key is invalid.
-
sign
Sign the given content using this Algorithm instance.- Parameters:
headerBytes- an array of bytes representing the base64 encoded header content to be verified against the signature.payloadBytes- an array of bytes representing the base64 encoded payload content to be verified against the signature.- Returns:
- the signature in a base64 encoded array of bytes
- Throws:
SignatureGenerationException- if the Key is invalid.
-
sign
Deprecated.Please use the sign(byte[], byte[]) method instead.Sign the given content using this Algorithm instance.- Parameters:
contentBytes- an array of bytes representing the base64 encoded content to be verified against the signature.- Returns:
- the signature in a base64 encoded array of bytes
- Throws:
SignatureGenerationException- if the Key is invalid.
-
ECDSA256(ECPublicKey, ECPrivateKey)orECDSA256(ECDSAKeyProvider)