Class HMAC


  • @ThreadSafe
    public class HMAC
    extends Object
    Static methods for Hash-based Message Authentication Codes (HMAC). This class is thread-safe.
    Version:
    2023-09-14
    Author:
    Axel Nennker, Vladimir Dzhuvinov, Ulrich Winter
    • Constructor Detail

      • HMAC

        public HMAC()
    • Method Detail

      • getInitMac

        public static Mac getInitMac​(SecretKey secretKey,
                                     Provider provider)
                              throws JOSEException
        Gets an initialised Message Authentication Code (MAC) service instance.
        Parameters:
        secretKey - The secret key, with the appropriate HMAC algorithm. Must not be null.
        provider - The JCA provider, or null to use the default one.
        Returns:
        The MAC service instance.
        Throws:
        JOSEException - If the algorithm is not supported or the MAC secret key is invalid.
      • getInitMac

        public static Mac getInitMac​(String alg,
                                     SecretKey secretKey,
                                     Provider provider)
                              throws JOSEException
        Gets an initialised Message Authentication Code (MAC) service instance.
        Parameters:
        alg - The Java Cryptography Architecture (JCA) HMAC algorithm name. Must not be null.
        secretKey - The secret key. Its algorithm name is ignored. Must not be null.
        provider - The JCA provider, or null to use the default one.
        Returns:
        The MAC service instance.
        Throws:
        JOSEException - If the algorithm is not supported or the MAC secret key is invalid.
      • compute

        @Deprecated
        public static byte[] compute​(String alg,
                                     byte[] secret,
                                     byte[] message,
                                     Provider provider)
                              throws JOSEException
        Deprecated.
        Computes a Hash-based Message Authentication Code (HMAC) for the specified secret and message.
        Parameters:
        alg - The Java Cryptography Architecture (JCA) HMAC algorithm name. Must not be null.
        secret - The secret. Must not be null.
        message - The message. Must not be null.
        provider - The JCA provider, or null to use the default one.
        Returns:
        The computed HMAC.
        Throws:
        JOSEException - If the algorithm is not supported or the MAC secret key is invalid.
      • compute

        public static byte[] compute​(String alg,
                                     SecretKey secretKey,
                                     byte[] message,
                                     Provider provider)
                              throws JOSEException
        Computes a Hash-based Message Authentication Code (HMAC) for the specified secret key and message.
        Parameters:
        alg - The Java Cryptography Architecture (JCA) HMAC algorithm name. Must not be null.
        secretKey - The secret key. Its algorithm name is ignored. Must not be null.
        message - The message. Must not be null.
        provider - The JCA provider, or null to use the default one.
        Returns:
        The computed HMAC.
        Throws:
        JOSEException - If the algorithm is not supported or the MAC secret key is invalid.
      • compute

        public static byte[] compute​(SecretKey secretKey,
                                     byte[] message,
                                     Provider provider)
                              throws JOSEException
        Computes a Hash-based Message Authentication Code (HMAC) for the specified secret key and message.
        Parameters:
        secretKey - The secret key, with the appropriate HMAC algorithm. Must not be null.
        message - The message. Must not be null.
        provider - The JCA provider, or null to use the default one.
        Returns:
        A MAC service instance.
        Throws:
        JOSEException - If the algorithm is not supported or the MAC secret key is invalid.