Class HmacHashFunction
java.lang.Object
cloud.opencode.base.hash.function.AbstractHashFunction
cloud.opencode.base.hash.function.HmacHashFunction
- All Implemented Interfaces:
HashFunction
HMAC hash function implementation
HMAC 哈希函数实现
Wraps Mac to provide HMAC-based hash functions
supporting MD5, SHA-1, SHA-256, SHA-384, and SHA-512 algorithms.
封装 Mac,提供基于 HMAC 的哈希函数,
支持 MD5、SHA-1、SHA-256、SHA-384 和 SHA-512 算法。
Features | 主要功能:
- HMAC-MD5, HMAC-SHA1, HMAC-SHA256, HMAC-SHA384, HMAC-SHA512 - 支持多种HMAC算法
- Key cloned internally, never exposed - 密钥内部克隆,不对外暴露
- Each newHasher() creates a new Mac instance - 每次newHasher()创建新的Mac实例
Usage Examples | 使用示例:
byte[] key = "secret".getBytes(StandardCharsets.UTF_8);
HashFunction hmac = HmacHashFunction.hmacSha256(key);
HashCode hash = hmac.hashUtf8("Hello World");
String hex = hash.toHex();
// Streaming via Hasher
Hasher hasher = hmac.newHasher();
hasher.putUtf8("Hello").putInt(42);
HashCode streamHash = hasher.hash();
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Key never exposed via toString() - 密钥不会通过toString()暴露
- Suitable for message authentication - 适用于消息认证
- Since:
- JDK 25, opencode-base-hash V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractHashFunction
AbstractHashFunction.AbstractHasher, AbstractHashFunction.BufferedHasher -
Field Summary
Fields inherited from class AbstractHashFunction
bits, name -
Method Summary
Modifier and TypeMethodDescriptionhashBytes(byte[] input, int offset, int length) Computes hash of a byte array portion 计算字节数组部分的哈希static HmacHashFunctionhmacMd5(byte[] key) Creates an HMAC-MD5 hash function 创建 HMAC-MD5 哈希函数static HmacHashFunctionhmacSha1(byte[] key) Creates an HMAC-SHA1 hash function 创建 HMAC-SHA1 哈希函数static HmacHashFunctionhmacSha256(byte[] key) Creates an HMAC-SHA256 hash function 创建 HMAC-SHA256 哈希函数static HmacHashFunctionhmacSha384(byte[] key) Creates an HMAC-SHA384 hash function 创建 HMAC-SHA384 哈希函数static HmacHashFunctionhmacSha512(byte[] key) Creates an HMAC-SHA512 hash function 创建 HMAC-SHA512 哈希函数Creates a new Hasher instance for streaming hash computation 创建新的Hasher实例用于流式哈希计算toString()Methods inherited from class AbstractHashFunction
bits, hashBytes, hashInt, hashLong, hashObject, name, newHasherMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface HashFunction
hashFile, hashInputStream, hashString, hashUtf8
-
Method Details
-
hmacMd5
Creates an HMAC-MD5 hash function 创建 HMAC-MD5 哈希函数- Parameters:
key- secret key bytes | 密钥字节- Returns:
- HMAC-MD5 hash function | HMAC-MD5 哈希函数
- Throws:
OpenHashException- if key is null or empty | 如果密钥为null或为空
-
hmacSha1
Creates an HMAC-SHA1 hash function 创建 HMAC-SHA1 哈希函数- Parameters:
key- secret key bytes | 密钥字节- Returns:
- HMAC-SHA1 hash function | HMAC-SHA1 哈希函数
- Throws:
OpenHashException- if key is null or empty | 如果密钥为null或为空
-
hmacSha256
Creates an HMAC-SHA256 hash function 创建 HMAC-SHA256 哈希函数- Parameters:
key- secret key bytes | 密钥字节- Returns:
- HMAC-SHA256 hash function | HMAC-SHA256 哈希函数
- Throws:
OpenHashException- if key is null or empty | 如果密钥为null或为空
-
hmacSha384
Creates an HMAC-SHA384 hash function 创建 HMAC-SHA384 哈希函数- Parameters:
key- secret key bytes | 密钥字节- Returns:
- HMAC-SHA384 hash function | HMAC-SHA384 哈希函数
- Throws:
OpenHashException- if key is null or empty | 如果密钥为null或为空
-
hmacSha512
Creates an HMAC-SHA512 hash function 创建 HMAC-SHA512 哈希函数- Parameters:
key- secret key bytes | 密钥字节- Returns:
- HMAC-SHA512 hash function | HMAC-SHA512 哈希函数
- Throws:
OpenHashException- if key is null or empty | 如果密钥为null或为空
-
newHasher
Description copied from interface:HashFunctionCreates a new Hasher instance for streaming hash computation 创建新的Hasher实例用于流式哈希计算- Returns:
- a new Hasher (stateful, not thread-safe) | 新的Hasher(有状态,非线程安全)
-
hashBytes
Description copied from interface:HashFunctionComputes hash of a byte array portion 计算字节数组部分的哈希- Parameters:
input- input bytes | 输入字节offset- starting offset | 起始偏移length- number of bytes to hash | 要哈希的字节数- Returns:
- hash code | 哈希码
-
toString
- Overrides:
toStringin classAbstractHashFunction
-