Class HmacHashFunction

java.lang.Object
cloud.opencode.base.hash.function.AbstractHashFunction
cloud.opencode.base.hash.function.HmacHashFunction
All Implemented Interfaces:
HashFunction

public final class HmacHashFunction extends AbstractHashFunction
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:
  • Method Details

    • hmacMd5

      public static HmacHashFunction hmacMd5(byte[] key)
      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

      public static HmacHashFunction hmacSha1(byte[] key)
      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

      public static HmacHashFunction hmacSha256(byte[] key)
      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

      public static HmacHashFunction hmacSha384(byte[] key)
      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

      public static HmacHashFunction hmacSha512(byte[] key)
      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

      public Hasher newHasher()
      Description copied from interface: HashFunction
      Creates a new Hasher instance for streaming hash computation 创建新的Hasher实例用于流式哈希计算
      Returns:
      a new Hasher (stateful, not thread-safe) | 新的Hasher(有状态,非线程安全)
    • hashBytes

      public HashCode hashBytes(byte[] input, int offset, int length)
      Description copied from interface: HashFunction
      Computes hash of a byte array portion 计算字节数组部分的哈希
      Parameters:
      input - input bytes | 输入字节
      offset - starting offset | 起始偏移
      length - number of bytes to hash | 要哈希的字节数
      Returns:
      hash code | 哈希码
    • toString

      public String toString()
      Overrides:
      toString in class AbstractHashFunction