Class Algorithm

    • Field Detail

      • name

        private final java.lang.String name
      • description

        private final java.lang.String description
    • Constructor Detail

      • Algorithm

        protected Algorithm​(java.lang.String name,
                            java.lang.String description)
    • 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
        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
        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
        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
        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
        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
        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.
      • 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:
        toString in class java.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 SignatureGenerationException
        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
        public abstract byte[] sign​(byte[] contentBytes)
                             throws SignatureGenerationException
        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.