Class JcaHmac
java.lang.Object
cloud.opencode.base.crypto.mac.JcaHmac
- All Implemented Interfaces:
Mac
Generic JCA HMAC wrapper for any algorithm name supported by the JDK provider.
Covers legacy / infrequent variants (HmacMD5, HmacSHA1, HmacSHA384) not served
by the algorithm-specific classes
HmacSha256 / HmacSha512.
通用 JCA HMAC 包装器,委托 JDK 提供的任意 HMAC 算法名。
用于覆盖 HmacSha256 / HmacSha512 未覆盖的遗留/次要变体
(HmacMD5、HmacSHA1、HmacSHA384 等)。
Features | 主要功能:
- Arbitrary JCA HMAC algorithm - 任意 JCA HMAC 算法
- Constant-time verification - 常量时间验证
Usage Examples | 使用示例:
Mac mac = JcaHmac.of("HmacSHA1", key);
String hex = mac.computeHex("payload".getBytes());
Security | 安全性:
- Thread-safe: No (reuse creates shared state) - 线程安全: 否(实例有共享状态)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-crypto V1.0.4
- 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 长度(字节数)static JcaHmacCreates a new HMAC instance with the given algorithm name and raw key.static JcaHmacCreates a new HMAC instance with the given algorithm name andSecretKey.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[] macValue) 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
-
of
Creates a new HMAC instance with the given algorithm name and raw key. 使用指定算法名和原始密钥字节创建 HMAC 实例。- Parameters:
algorithm- JCA HMAC algorithm name (e.g."HmacMD5","HmacSHA1") | JCA HMAC 算法名key- secret key bytes | 密钥字节- Returns:
- new JcaHmac instance | 新实例
- Throws:
NullPointerException- ifalgorithmorkeyis null
-
of
Creates a new HMAC instance with the given algorithm name andSecretKey. 使用指定算法名和SecretKey创建 HMAC 实例。- Parameters:
algorithm- JCA HMAC algorithm name | JCA HMAC 算法名key- the secret key | 密钥- Returns:
- new JcaHmac instance | 新实例
- Throws:
NullPointerException- ifalgorithmorkeyis null
-
compute
-
compute
-
computeHex
Description copied from interface:MacComputes MAC and returns as hexadecimal string 计算 MAC 值并返回十六进制字符串- Specified by:
computeHexin interfaceMac- Parameters:
data- the data to authenticate- Returns:
- the MAC value as hex string
-
computeBase64
Description copied from interface:MacComputes MAC and returns as Base64 string 计算 MAC 值并返回 Base64 字符串- Specified by:
computeBase64in interfaceMac- 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:MacVerifies MAC for the given data using constant-time comparison 使用常量时间比较验证给定数据的 MAC 值 -
verifyHex
Description copied from interface:MacVerifies MAC from hexadecimal string using constant-time comparison 使用常量时间比较验证十六进制字符串的 MAC 值 -
verifyBase64
Description copied from interface:MacVerifies MAC from Base64 string using constant-time comparison 使用常量时间比较验证 Base64 字符串的 MAC 值- Specified by:
verifyBase64in interfaceMac- Parameters:
data- the data to verifymacBase64- the expected MAC value in Base64 format- Returns:
- true if MAC is valid, false otherwise
-
update
-
update
-
doFinal
-
doFinalHex
Description copied from interface:MacCompletes the MAC computation and returns as hexadecimal string 完成 MAC 计算并返回十六进制字符串- Specified by:
doFinalHexin interfaceMac- Returns:
- the final MAC value as hex string
-
doFinalBase64
Description copied from interface:MacCompletes the MAC computation and returns as Base64 string 完成 MAC 计算并返回 Base64 字符串- Specified by:
doFinalBase64in interfaceMac- Returns:
- the final MAC value as Base64 string
-
reset
-
getAlgorithm
Description copied from interface:MacReturns the algorithm name 返回算法名称- Specified by:
getAlgorithmin interfaceMac- Returns:
- the algorithm name
-
getMacLength
public int getMacLength()Description copied from interface:MacReturns the MAC length in bytes 返回 MAC 长度(字节数)- Specified by:
getMacLengthin interfaceMac- Returns:
- the MAC length
-