Class HmacSha256

java.lang.Object
cloud.opencode.base.crypto.mac.HmacSha256
All Implemented Interfaces:
Mac

public final class HmacSha256 extends Object implements Mac
HMAC-SHA256 implementation - Hash-based Message Authentication Code using SHA-256 HMAC-SHA256 实现 - 基于 SHA-256 的哈希消息认证码

Features | 主要功能:

  • HMAC-SHA256 message authentication - HMAC-SHA256 消息认证
  • Constant-time verification - 常量时间验证

Usage Examples | 使用示例:

Mac mac = HmacSha256.of(keyBytes);
byte[] tag = mac.compute(data);

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
    byte[]
    compute(byte[] data)
    Computes MAC for the given byte array 计算给定字节数组的 MAC 值
    byte[]
    Computes MAC for the given string (UTF-8 encoded) 计算给定字符串的 MAC 值(UTF-8 编码)
    computeBase64(byte[] data)
    Computes MAC and returns as Base64 string 计算 MAC 值并返回 Base64 字符串
    computeHex(byte[] data)
    Computes MAC and returns as hexadecimal string 计算 MAC 值并返回十六进制字符串
    byte[]
    Completes the MAC computation and returns the result 完成 MAC 计算并返回结果
    Completes the MAC computation and returns as Base64 string 完成 MAC 计算并返回 Base64 字符串
    Completes the MAC computation and returns as hexadecimal string 完成 MAC 计算并返回十六进制字符串
    Returns the algorithm name 返回算法名称
    int
    Returns the MAC length in bytes 返回 MAC 长度(字节数)
    static HmacSha256
    of(byte[] key)
    Creates a new HMAC-SHA256 instance with the given key 使用给定密钥创建新的 HMAC-SHA256 实例
    static HmacSha256
    Creates a new HMAC-SHA256 instance with the given SecretKey 使用给定的 SecretKey 创建新的 HMAC-SHA256 实例
    Resets the MAC to its initial state 将 MAC 重置为初始状态
    update(byte[] data)
    Updates the MAC with a chunk of data (for incremental computation) 使用数据块更新 MAC(用于增量计算)
    update(String data)
    Updates the MAC with a string (UTF-8 encoded) 使用字符串更新 MAC(UTF-8 编码)
    boolean
    verify(byte[] data, byte[] macValue)
    Verifies MAC for the given data using constant-time comparison 使用常量时间比较验证给定数据的 MAC 值
    boolean
    verifyBase64(byte[] data, String macBase64)
    Verifies MAC from Base64 string using constant-time comparison 使用常量时间比较验证 Base64 字符串的 MAC 值
    boolean
    verifyHex(byte[] data, String macHex)
    Verifies MAC from hexadecimal string using constant-time comparison 使用常量时间比较验证十六进制字符串的 MAC 值

    Methods inherited from class Object

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

    • of

      public static HmacSha256 of(byte[] key)
      Creates a new HMAC-SHA256 instance with the given key 使用给定密钥创建新的 HMAC-SHA256 实例
      Parameters:
      key - the secret key bytes
      Returns:
      new HmacSha256 instance
      Throws:
      NullPointerException - if key is null
    • of

      public static HmacSha256 of(SecretKey key)
      Creates a new HMAC-SHA256 instance with the given SecretKey 使用给定的 SecretKey 创建新的 HMAC-SHA256 实例
      Parameters:
      key - the secret key
      Returns:
      new HmacSha256 instance
      Throws:
      NullPointerException - if key is null
    • compute

      public byte[] compute(byte[] data)
      Description copied from interface: Mac
      Computes MAC for the given byte array 计算给定字节数组的 MAC 值
      Specified by:
      compute in interface Mac
      Parameters:
      data - the data to authenticate
      Returns:
      the MAC value
    • compute

      public byte[] compute(String data)
      Description copied from interface: Mac
      Computes MAC for the given string (UTF-8 encoded) 计算给定字符串的 MAC 值(UTF-8 编码)
      Specified by:
      compute in interface Mac
      Parameters:
      data - the string to authenticate
      Returns:
      the MAC value
    • computeHex

      public String computeHex(byte[] data)
      Description copied from interface: Mac
      Computes MAC and returns as hexadecimal string 计算 MAC 值并返回十六进制字符串
      Specified by:
      computeHex in interface Mac
      Parameters:
      data - the data to authenticate
      Returns:
      the MAC value as hex string
    • computeBase64

      public String computeBase64(byte[] data)
      Description copied from interface: Mac
      Computes MAC and returns as Base64 string 计算 MAC 值并返回 Base64 字符串
      Specified by:
      computeBase64 in interface Mac
      Parameters:
      data - the data to authenticate
      Returns:
      the MAC value as Base64 string
    • verify

      public boolean verify(byte[] data, byte[] macValue)
      Description copied from interface: Mac
      Verifies MAC for the given data using constant-time comparison 使用常量时间比较验证给定数据的 MAC 值
      Specified by:
      verify in interface Mac
      Parameters:
      data - the data to verify
      macValue - the expected MAC value
      Returns:
      true if MAC is valid, false otherwise
    • verifyHex

      public boolean verifyHex(byte[] data, String macHex)
      Description copied from interface: Mac
      Verifies MAC from hexadecimal string using constant-time comparison 使用常量时间比较验证十六进制字符串的 MAC 值
      Specified by:
      verifyHex in interface Mac
      Parameters:
      data - the data to verify
      macHex - the expected MAC value in hex format
      Returns:
      true if MAC is valid, false otherwise
    • verifyBase64

      public boolean verifyBase64(byte[] data, String macBase64)
      Description copied from interface: Mac
      Verifies MAC from Base64 string using constant-time comparison 使用常量时间比较验证 Base64 字符串的 MAC 值
      Specified by:
      verifyBase64 in interface Mac
      Parameters:
      data - the data to verify
      macBase64 - the expected MAC value in Base64 format
      Returns:
      true if MAC is valid, false otherwise
    • update

      public Mac update(byte[] data)
      Description copied from interface: Mac
      Updates the MAC with a chunk of data (for incremental computation) 使用数据块更新 MAC(用于增量计算)
      Specified by:
      update in interface Mac
      Parameters:
      data - the data chunk to add
      Returns:
      this MAC instance for chaining
    • update

      public Mac update(String data)
      Description copied from interface: Mac
      Updates the MAC with a string (UTF-8 encoded) 使用字符串更新 MAC(UTF-8 编码)
      Specified by:
      update in interface Mac
      Parameters:
      data - the string to add
      Returns:
      this MAC instance for chaining
    • doFinal

      public byte[] doFinal()
      Description copied from interface: Mac
      Completes the MAC computation and returns the result 完成 MAC 计算并返回结果
      Specified by:
      doFinal in interface Mac
      Returns:
      the final MAC value
    • doFinalHex

      public String doFinalHex()
      Description copied from interface: Mac
      Completes the MAC computation and returns as hexadecimal string 完成 MAC 计算并返回十六进制字符串
      Specified by:
      doFinalHex in interface Mac
      Returns:
      the final MAC value as hex string
    • doFinalBase64

      public String doFinalBase64()
      Description copied from interface: Mac
      Completes the MAC computation and returns as Base64 string 完成 MAC 计算并返回 Base64 字符串
      Specified by:
      doFinalBase64 in interface Mac
      Returns:
      the final MAC value as Base64 string
    • reset

      public Mac reset()
      Description copied from interface: Mac
      Resets the MAC to its initial state 将 MAC 重置为初始状态
      Specified by:
      reset in interface Mac
      Returns:
      this MAC instance for chaining
    • getAlgorithm

      public String getAlgorithm()
      Description copied from interface: Mac
      Returns the algorithm name 返回算法名称
      Specified by:
      getAlgorithm in interface Mac
      Returns:
      the algorithm name
    • getMacLength

      public int getMacLength()
      Description copied from interface: Mac
      Returns the MAC length in bytes 返回 MAC 长度(字节数)
      Specified by:
      getMacLength in interface Mac
      Returns:
      the MAC length