Package com.auth0.jwt.algorithms
Class Algorithm
- java.lang.Object
-
- com.auth0.jwt.algorithms.Algorithm
-
- Direct Known Subclasses:
ECDSAAlgorithm,HMACAlgorithm,NoneAlgorithm,RSAAlgorithm
public abstract class Algorithm extends java.lang.ObjectThe Algorithm class represents an algorithm to be used in the Signing or Verification process of a Token.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Stringdescriptionprivate java.lang.Stringname
-
Constructor Summary
Constructors Modifier Constructor Description protectedAlgorithm(java.lang.String name, java.lang.String description)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static AlgorithmECDSA256(ECDSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA256withECDSA.static AlgorithmECDSA256(java.security.interfaces.ECKey key)Deprecated.static AlgorithmECDSA256(java.security.interfaces.ECPublicKey publicKey, java.security.interfaces.ECPrivateKey privateKey)Creates a new Algorithm instance using SHA256withECDSA.static AlgorithmECDSA384(ECDSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA384withECDSA.static AlgorithmECDSA384(java.security.interfaces.ECKey key)Deprecated.static AlgorithmECDSA384(java.security.interfaces.ECPublicKey publicKey, java.security.interfaces.ECPrivateKey privateKey)Creates a new Algorithm instance using SHA384withECDSA.static AlgorithmECDSA512(ECDSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA512withECDSA.static AlgorithmECDSA512(java.security.interfaces.ECKey key)Deprecated.static AlgorithmECDSA512(java.security.interfaces.ECPublicKey publicKey, java.security.interfaces.ECPrivateKey privateKey)Creates a new Algorithm instance using SHA512withECDSA.(package private) java.lang.StringgetDescription()Getter for the description of this Algorithm, required when instantiating a Mac or Signature object.java.lang.StringgetName()Getter for the name of this Algorithm, as defined in the JWT Standard.java.lang.StringgetSigningKeyId()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 AlgorithmHMAC256(java.lang.String secret)Creates a new Algorithm instance using HmacSHA256.static AlgorithmHMAC384(byte[] secret)Creates a new Algorithm instance using HmacSHA384.static AlgorithmHMAC384(java.lang.String secret)Creates a new Algorithm instance using HmacSHA384.static AlgorithmHMAC512(byte[] secret)Creates a new Algorithm instance using HmacSHA512.static AlgorithmHMAC512(java.lang.String secret)Creates a new Algorithm instance using HmacSHA512.static Algorithmnone()static AlgorithmRSA256(RSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA256withRSA.static AlgorithmRSA256(java.security.interfaces.RSAKey key)Deprecated.static AlgorithmRSA256(java.security.interfaces.RSAPublicKey publicKey, java.security.interfaces.RSAPrivateKey privateKey)Creates a new Algorithm instance using SHA256withRSA.static AlgorithmRSA384(RSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA384withRSA.static AlgorithmRSA384(java.security.interfaces.RSAKey key)Deprecated.static AlgorithmRSA384(java.security.interfaces.RSAPublicKey publicKey, java.security.interfaces.RSAPrivateKey privateKey)Creates a new Algorithm instance using SHA384withRSA.static AlgorithmRSA512(RSAKeyProvider keyProvider)Creates a new Algorithm instance using SHA512withRSA.static AlgorithmRSA512(java.security.interfaces.RSAKey key)Deprecated.static AlgorithmRSA512(java.security.interfaces.RSAPublicKey publicKey, java.security.interfaces.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.java.lang.StringtoString()abstract voidverify(DecodedJWT jwt)Verify the given token using this Algorithm instance.
-
-
-
Method Detail
-
RSA256
public static Algorithm RSA256(RSAKeyProvider keyProvider) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Key is null.
-
RSA256
public static Algorithm RSA256(java.security.interfaces.RSAPublicKey publicKey, java.security.interfaces.RSAPrivateKey privateKey) throws java.lang.IllegalArgumentException
Creates 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:
java.lang.IllegalArgumentException- if both provided Keys are null.
-
RSA256
@Deprecated public static Algorithm RSA256(java.security.interfaces.RSAKey key) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the Key Provider is null.
-
RSA384
public static Algorithm RSA384(RSAKeyProvider keyProvider) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the Key Provider is null.
-
RSA384
public static Algorithm RSA384(java.security.interfaces.RSAPublicKey publicKey, java.security.interfaces.RSAPrivateKey privateKey) throws java.lang.IllegalArgumentException
Creates 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:
java.lang.IllegalArgumentException- if both provided Keys are null.
-
RSA384
@Deprecated public static Algorithm RSA384(java.security.interfaces.RSAKey key) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Key is null.
-
RSA512
public static Algorithm RSA512(RSAKeyProvider keyProvider) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the Key Provider is null.
-
RSA512
public static Algorithm RSA512(java.security.interfaces.RSAPublicKey publicKey, java.security.interfaces.RSAPrivateKey privateKey) throws java.lang.IllegalArgumentException
Creates 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:
java.lang.IllegalArgumentException- if both provided Keys are null.
-
RSA512
@Deprecated public static Algorithm RSA512(java.security.interfaces.RSAKey key) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Key is null.
-
HMAC256
public static Algorithm HMAC256(java.lang.String secret) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Secret is null.
-
HMAC384
public static Algorithm HMAC384(java.lang.String secret) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Secret is null.
-
HMAC512
public static Algorithm HMAC512(java.lang.String secret) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Secret is null.
-
HMAC256
public static Algorithm HMAC256(byte[] secret) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Secret is null.
-
HMAC384
public static Algorithm HMAC384(byte[] secret) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Secret is null.
-
HMAC512
public static Algorithm HMAC512(byte[] secret) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Secret is null.
-
ECDSA256
public static Algorithm ECDSA256(ECDSAKeyProvider keyProvider) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the Key Provider is null.
-
ECDSA256
public static Algorithm ECDSA256(java.security.interfaces.ECPublicKey publicKey, java.security.interfaces.ECPrivateKey privateKey) throws java.lang.IllegalArgumentException
Creates 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:
java.lang.IllegalArgumentException- if the provided Key is null.
-
ECDSA256
@Deprecated public static Algorithm ECDSA256(java.security.interfaces.ECKey key) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Key is null.
-
ECDSA384
public static Algorithm ECDSA384(ECDSAKeyProvider keyProvider) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the Key Provider is null.
-
ECDSA384
public static Algorithm ECDSA384(java.security.interfaces.ECPublicKey publicKey, java.security.interfaces.ECPrivateKey privateKey) throws java.lang.IllegalArgumentException
Creates 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:
java.lang.IllegalArgumentException- if the provided Key is null.
-
ECDSA384
@Deprecated public static Algorithm ECDSA384(java.security.interfaces.ECKey key) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Key is null.
-
ECDSA512
public static Algorithm ECDSA512(ECDSAKeyProvider keyProvider) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the Key Provider is null.
-
ECDSA512
public static Algorithm ECDSA512(java.security.interfaces.ECPublicKey publicKey, java.security.interfaces.ECPrivateKey privateKey) throws java.lang.IllegalArgumentException
Creates 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:
java.lang.IllegalArgumentException- if the provided Key is null.
-
ECDSA512
@Deprecated public static Algorithm ECDSA512(java.security.interfaces.ECKey key) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException- if the provided Key is null.
-
none
public static Algorithm none()
-
getSigningKeyId
public java.lang.String 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
public java.lang.String getName()
Getter for the name of this Algorithm, as defined in the JWT Standard. i.e. "HS256"- Returns:
- the algorithm name.
-
getDescription
java.lang.String getDescription()
Getter for the description of this Algorithm, required when instantiating a Mac or Signature object. i.e. "HmacSHA256"- Returns:
- the algorithm description.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
verify
public abstract void verify(DecodedJWT jwt) throws SignatureVerificationException
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
public byte[] sign(byte[] headerBytes, byte[] payloadBytes) throws SignatureGenerationExceptionSign 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 public abstract byte[] sign(byte[] contentBytes) throws SignatureGenerationExceptionDeprecated.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.
-
-