Interface Mac

All Known Implementing Classes:
HmacSha256, HmacSha512, Poly1305

public interface Mac
Message Authentication Code (MAC) interface - Provides cryptographic message authentication 消息认证码接口 - 提供加密消息认证功能

Features | 主要功能:

  • Message authentication code computation - 消息认证码计算
  • Hex and Base64 output encoding - 十六进制和 Base64 输出编码
  • Constant-time verification - 常量时间验证

Usage Examples | 使用示例:

Mac mac = HmacSha256.of(key);
byte[] tag = mac.compute("message".getBytes());
boolean valid = mac.verify("message".getBytes(), tag);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Partial - 空值安全: 部分
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 长度(字节数)
    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[] mac)
    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 值
  • Method Details

    • compute

      byte[] compute(byte[] data)
      Computes MAC for the given byte array 计算给定字节数组的 MAC 值
      Parameters:
      data - the data to authenticate
      Returns:
      the MAC value
    • compute

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

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

      String computeBase64(byte[] data)
      Computes MAC and returns as Base64 string 计算 MAC 值并返回 Base64 字符串
      Parameters:
      data - the data to authenticate
      Returns:
      the MAC value as Base64 string
    • verify

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

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

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

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

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

      byte[] doFinal()
      Completes the MAC computation and returns the result 完成 MAC 计算并返回结果
      Returns:
      the final MAC value
    • doFinalHex

      String doFinalHex()
      Completes the MAC computation and returns as hexadecimal string 完成 MAC 计算并返回十六进制字符串
      Returns:
      the final MAC value as hex string
    • doFinalBase64

      String doFinalBase64()
      Completes the MAC computation and returns as Base64 string 完成 MAC 计算并返回 Base64 字符串
      Returns:
      the final MAC value as Base64 string
    • reset

      Mac reset()
      Resets the MAC to its initial state 将 MAC 重置为初始状态
      Returns:
      this MAC instance for chaining
    • getAlgorithm

      String getAlgorithm()
      Returns the algorithm name 返回算法名称
      Returns:
      the algorithm name
    • getMacLength

      int getMacLength()
      Returns the MAC length in bytes 返回 MAC 长度(字节数)
      Returns:
      the MAC length