Class HMAC

java.lang.Object
com.gmt2001.security.HMAC

public final class HMAC extends Object
Provides an easy interface to calculating an HMAC-SHA256
Author:
Søren Poulsen and gmt2001
  • Method Details

    • calcHmacSha256

      public static byte[] calcHmacSha256(byte[] secretKey, byte[] message)
      Calculates an HMAC-SHA256
      Parameters:
      secretKey - The secret key
      message - The message to calculate the MAC on
      Returns:
      the MAC
    • calcHmacSha256

      public static String calcHmacSha256(String secretKey, String message)
      Calculates an HMAC-SHA256 and returns the MAC as a hex-encoded string
      Parameters:
      secretKey - The secret key
      message - The message to calculate the MAC on
      Returns:
      the MAC as a hex-encoded string
    • compareHmacSha256

      public static boolean compareHmacSha256(byte[] secretKey, byte[] message, byte[] mac)
      Calculates an HMAC-SHA256 and compares it to an existing MAC
      Parameters:
      secretKey - The secret key
      message - The message to calculate the MAC on
      mac - The MAC to compare to
      Returns:
      true if the calculated MAC matches the provided MAC
    • compareHmacSha256

      public static boolean compareHmacSha256(String secretKey, String message, String mac)
      Calculates an HMAC-SHA256 and compares it to an existing MAC
      Parameters:
      secretKey - The secret key
      message - The message to calculate the MAC on
      mac - The MAC to compare to
      Returns:
      true if the calculated MAC matches the provided MAC