Class HmacFunction

java.lang.Object
cloud.opencode.base.crypto.hash.HmacFunction

public final class HmacFunction extends Object
HMAC (Hash-based Message Authentication Code) function implementation - Keyed hash for message authentication HMAC(基于哈希的消息认证码)函数实现 - 用于消息认证的密钥哈希

Features | 主要功能:

  • HMAC-SHA256, HMAC-SHA384, HMAC-SHA512 - HMAC-SHA256、HMAC-SHA384、HMAC-SHA512
  • Constant-time verification - 常量时间验证
  • Hex and Base64 output encoding - 十六进制和 Base64 输出编码

Usage Examples | 使用示例:

HmacFunction hmac = HmacFunction.hmacSha256(keyBytes);
String hex = hmac.computeHex("message");
boolean valid = hmac.verify("message".getBytes(), expectedMac);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Get the HMAC algorithm name 获取 HMAC 算法名称
    hmacSha256(byte[] key)
    Create HMAC-SHA256 function 创建 HMAC-SHA256 函数
    hmacSha384(byte[] key)
    Create HMAC-SHA384 function 创建 HMAC-SHA384 函数
    hmacSha512(byte[] key)
    Create HMAC-SHA512 function 创建 HMAC-SHA512 函数
    byte[]
    mac(byte[] data)
    Compute MAC (Message Authentication Code) of byte array 计算字节数组的消息认证码
    byte[]
    mac(String data)
    Compute MAC of string (UTF-8 encoded) 计算字符串的消息认证码(使用 UTF-8 编码)
    macBase64(byte[] data)
    Compute MAC and return as Base64 string 计算消息认证码并返回 Base64 字符串
    macHex(byte[] data)
    Compute MAC and return as hexadecimal string 计算消息认证码并返回十六进制字符串
    of(String algorithm, byte[] key)
    Create HMAC function with custom algorithm 创建指定算法的 HMAC 函数
    boolean
    verify(byte[] data, byte[] mac)
    Verify MAC for given data using constant-time comparison 使用恒定时间比较验证给定数据的消息认证码

    Methods inherited from class Object

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

    • hmacSha256

      public static HmacFunction hmacSha256(byte[] key)
      Create HMAC-SHA256 function 创建 HMAC-SHA256 函数
      Parameters:
      key - secret key for HMAC
      Returns:
      HMAC-SHA256 function instance
      Throws:
      NullPointerException - if key is null
      IllegalArgumentException - if key is empty
    • hmacSha384

      public static HmacFunction hmacSha384(byte[] key)
      Create HMAC-SHA384 function 创建 HMAC-SHA384 函数
      Parameters:
      key - secret key for HMAC
      Returns:
      HMAC-SHA384 function instance
      Throws:
      NullPointerException - if key is null
      IllegalArgumentException - if key is empty
    • hmacSha512

      public static HmacFunction hmacSha512(byte[] key)
      Create HMAC-SHA512 function 创建 HMAC-SHA512 函数
      Parameters:
      key - secret key for HMAC
      Returns:
      HMAC-SHA512 function instance
      Throws:
      NullPointerException - if key is null
      IllegalArgumentException - if key is empty
    • of

      public static HmacFunction of(String algorithm, byte[] key)
      Create HMAC function with custom algorithm 创建指定算法的 HMAC 函数
      Parameters:
      algorithm - HMAC algorithm name (e.g., "HmacSHA256", "HmacSHA512")
      key - secret key for HMAC
      Returns:
      HMAC function instance
      Throws:
      NullPointerException - if algorithm or key is null
      IllegalArgumentException - if key is empty
    • mac

      public byte[] mac(byte[] data)
      Compute MAC (Message Authentication Code) of byte array 计算字节数组的消息认证码
      Parameters:
      data - input data to authenticate
      Returns:
      MAC as byte array
      Throws:
      NullPointerException - if data is null
      OpenCryptoException - if MAC computation fails
    • mac

      public byte[] mac(String data)
      Compute MAC of string (UTF-8 encoded) 计算字符串的消息认证码(使用 UTF-8 编码)
      Parameters:
      data - input string to authenticate
      Returns:
      MAC as byte array
      Throws:
      NullPointerException - if data is null
      OpenCryptoException - if MAC computation fails
    • macHex

      public String macHex(byte[] data)
      Compute MAC and return as hexadecimal string 计算消息认证码并返回十六进制字符串
      Parameters:
      data - input data to authenticate
      Returns:
      MAC as lowercase hex string
      Throws:
      NullPointerException - if data is null
      OpenCryptoException - if MAC computation fails
    • macBase64

      public String macBase64(byte[] data)
      Compute MAC and return as Base64 string 计算消息认证码并返回 Base64 字符串
      Parameters:
      data - input data to authenticate
      Returns:
      MAC as Base64 string
      Throws:
      NullPointerException - if data is null
      OpenCryptoException - if MAC computation fails
    • verify

      public boolean verify(byte[] data, byte[] mac)
      Verify MAC for given data using constant-time comparison 使用恒定时间比较验证给定数据的消息认证码
      Parameters:
      data - original data
      mac - MAC to verify
      Returns:
      true if MAC is valid, false otherwise
      Throws:
      NullPointerException - if data or mac is null
      OpenCryptoException - if MAC computation fails
    • getAlgorithm

      public String getAlgorithm()
      Get the HMAC algorithm name 获取 HMAC 算法名称
      Returns:
      algorithm name