Class HMACUtils

java.lang.Object
dev.vink.jtotp.HMACUtils

public class HMACUtils extends Object
Utility class for generating HMAC (Hash-based Message Authentication Code) and converting byte arrays to hexadecimal strings.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Algorithm input for TOTP Builder for HMAC-SHA1.
    static final String
    Algorithm input for TOTP Builder for HMAC-SHA256.
    static final String
    Algorithm input for TOTP Builder for HMAC-SHA512.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    calculateHMAC(byte[] key, byte[] data)
    Calculates the HMAC of the given data using the default HMAC algorithm (HmacSHA1).
    static byte[]
    calculateHMAC(byte[] key, byte[] data, String algorithm)
    Calculates the HMAC of the given data using the specified key and algorithm.
    static String
    toHex(byte[] bytes)
    Converts a byte array into a hexadecimal string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SHA1_ALGORITHM

      public static final String SHA1_ALGORITHM
      Algorithm input for TOTP Builder for HMAC-SHA1.
      See Also:
    • SHA256_ALGORITHM

      public static final String SHA256_ALGORITHM
      Algorithm input for TOTP Builder for HMAC-SHA256.
      See Also:
    • SHA512_ALGORITHM

      public static final String SHA512_ALGORITHM
      Algorithm input for TOTP Builder for HMAC-SHA512.
      See Also:
  • Method Details

    • calculateHMAC

      public static byte[] calculateHMAC(byte[] key, byte[] data, String algorithm) throws NoSuchAlgorithmException, InvalidKeyException
      Calculates the HMAC of the given data using the specified key and algorithm.
      Parameters:
      key - The secret key used for HMAC generation as a byte array.
      data - The data to be hashed as a byte array.
      algorithm - The HMAC algorithm to use (e.g., HmacSHA1, HmacSHA256, HmacSHA512).
      Returns:
      A byte array representing the HMAC of the input data.
      Throws:
      NoSuchAlgorithmException - If the HMAC algorithm is not available.
      InvalidKeyException - If the provided key is invalid.
    • calculateHMAC

      public static byte[] calculateHMAC(byte[] key, byte[] data) throws NoSuchAlgorithmException, InvalidKeyException
      Calculates the HMAC of the given data using the default HMAC algorithm (HmacSHA1).
      Parameters:
      key - The secret key used for HMAC generation as a byte array.
      data - The data to be hashed as a byte array.
      Returns:
      A byte array representing the HMAC of the input data.
      Throws:
      NoSuchAlgorithmException - If the HMAC algorithm is not available.
      InvalidKeyException - If the provided key is invalid.
    • toHex

      public static String toHex(byte[] bytes)
      Converts a byte array into a hexadecimal string.
      Parameters:
      bytes - The byte array to convert.
      Returns:
      A string representing the hexadecimal representation of the byte array.