Class HmacFunction
java.lang.Object
cloud.opencode.base.crypto.hash.HmacFunction
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 TypeMethodDescriptionGet the HMAC algorithm name 获取 HMAC 算法名称static HmacFunctionhmacSha256(byte[] key) Create HMAC-SHA256 function 创建 HMAC-SHA256 函数static HmacFunctionhmacSha384(byte[] key) Create HMAC-SHA384 function 创建 HMAC-SHA384 函数static HmacFunctionhmacSha512(byte[] key) Create HMAC-SHA512 function 创建 HMAC-SHA512 函数byte[]mac(byte[] data) Compute MAC (Message Authentication Code) of byte array 计算字节数组的消息认证码byte[]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 计算消息认证码并返回十六进制字符串static HmacFunctionCreate HMAC function with custom algorithm 创建指定算法的 HMAC 函数booleanverify(byte[] data, byte[] mac) Verify MAC for given data using constant-time comparison 使用恒定时间比较验证给定数据的消息认证码
-
Method Details
-
hmacSha256
Create HMAC-SHA256 function 创建 HMAC-SHA256 函数- Parameters:
key- secret key for HMAC- Returns:
- HMAC-SHA256 function instance
- Throws:
NullPointerException- if key is nullIllegalArgumentException- if key is empty
-
hmacSha384
Create HMAC-SHA384 function 创建 HMAC-SHA384 函数- Parameters:
key- secret key for HMAC- Returns:
- HMAC-SHA384 function instance
- Throws:
NullPointerException- if key is nullIllegalArgumentException- if key is empty
-
hmacSha512
Create HMAC-SHA512 function 创建 HMAC-SHA512 函数- Parameters:
key- secret key for HMAC- Returns:
- HMAC-SHA512 function instance
- Throws:
NullPointerException- if key is nullIllegalArgumentException- if key is empty
-
of
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 nullIllegalArgumentException- 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 nullOpenCryptoException- if MAC computation fails
-
mac
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 nullOpenCryptoException- if MAC computation fails
-
macHex
Compute MAC and return as hexadecimal string 计算消息认证码并返回十六进制字符串- Parameters:
data- input data to authenticate- Returns:
- MAC as lowercase hex string
- Throws:
NullPointerException- if data is nullOpenCryptoException- if MAC computation fails
-
macBase64
Compute MAC and return as Base64 string 计算消息认证码并返回 Base64 字符串- Parameters:
data- input data to authenticate- Returns:
- MAC as Base64 string
- Throws:
NullPointerException- if data is nullOpenCryptoException- if MAC computation fails
-
verify
public boolean verify(byte[] data, byte[] mac) Verify MAC for given data using constant-time comparison 使用恒定时间比较验证给定数据的消息认证码- Parameters:
data- original datamac- MAC to verify- Returns:
- true if MAC is valid, false otherwise
- Throws:
NullPointerException- if data or mac is nullOpenCryptoException- if MAC computation fails
-
getAlgorithm
-