Interface Mac
- All Known Implementing Classes:
HmacSha256, HmacSha512, JcaHmac, 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 TypeMethodDescriptionbyte[]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[]doFinal()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 返回算法名称intReturns the MAC length in bytes 返回 MAC 长度(字节数)reset()Resets the MAC to its initial state 将 MAC 重置为初始状态update(byte[] data) Updates the MAC with a chunk of data (for incremental computation) 使用数据块更新 MAC(用于增量计算)Updates the MAC with a string (UTF-8 encoded) 使用字符串更新 MAC(UTF-8 编码)booleanverify(byte[] data, byte[] mac) Verifies MAC for the given data using constant-time comparison 使用常量时间比较验证给定数据的 MAC 值booleanverifyBase64(byte[] data, String macBase64) Verifies MAC from Base64 string using constant-time comparison 使用常量时间比较验证 Base64 字符串的 MAC 值booleanVerifies 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
Computes MAC for the given string (UTF-8 encoded) 计算给定字符串的 MAC 值(UTF-8 编码)- Parameters:
data- the string to authenticate- Returns:
- the MAC value
-
computeHex
Computes MAC and returns as hexadecimal string 计算 MAC 值并返回十六进制字符串- Parameters:
data- the data to authenticate- Returns:
- the MAC value as hex string
-
computeBase64
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 verifymac- the expected MAC value- Returns:
- true if MAC is valid, false otherwise
-
verifyHex
Verifies MAC from hexadecimal string using constant-time comparison 使用常量时间比较验证十六进制字符串的 MAC 值- Parameters:
data- the data to verifymacHex- the expected MAC value in hex format- Returns:
- true if MAC is valid, false otherwise
-
verifyBase64
Verifies MAC from Base64 string using constant-time comparison 使用常量时间比较验证 Base64 字符串的 MAC 值- Parameters:
data- the data to verifymacBase64- the expected MAC value in Base64 format- Returns:
- true if MAC is valid, false otherwise
-
update
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
-
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
-
getMacLength
int getMacLength()Returns the MAC length in bytes 返回 MAC 长度(字节数)- Returns:
- the MAC length
-