Class MessageDigestHashFunction

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

public final class MessageDigestHashFunction extends AbstractHashFunction
MessageDigest-based hash function implementation 基于 MessageDigest 的哈希函数实现

Wraps JDK's MessageDigest to provide cryptographic hash functions with a unified API. Supports MD5, SHA-1, SHA-256, SHA-512, and SHA-3.

封装 JDK 的 MessageDigest 以提供统一 API 的加密哈希函数。 支持 MD5、SHA-1、SHA-256、SHA-512 和 SHA-3。

Features | 主要功能:

  • MD5 (128-bit, not for security) - MD5(128位,不用于安全)
  • SHA-1 (160-bit, not for security) - SHA-1(160位,不用于安全)
  • SHA-256 (256-bit) - SHA-256(256位)
  • SHA-512 (512-bit) - SHA-512(512位)
  • SHA3-256 (256-bit) - SHA3-256(256位)

Usage Examples | 使用示例:

// SHA-256
HashCode hash = MessageDigestHashFunction.sha256().hashUtf8("Hello");

// SHA3-256
HashCode sha3 = MessageDigestHashFunction.sha3_256().hashBytes(data);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • MD5/SHA-1 for checksums only - MD5/SHA-1 仅用于校验

Performance | 性能特性:

  • Time complexity: O(n) where n = input size - O(n), n为输入大小
  • Space complexity: O(1) for digest state - 摘要状态 O(1)
Since:
JDK 25, opencode-base-hash V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • md5

      public static MessageDigestHashFunction md5()
      Creates an MD5 function (128-bit) 创建 MD5 函数(128位)

      Warning: MD5 is cryptographically broken. Use only for checksums.

      警告: MD5 已被破解。仅用于校验。

      Returns:
      hash function | 哈希函数
    • sha1

      public static MessageDigestHashFunction sha1()
      Creates a SHA-1 function (160-bit) 创建 SHA-1 函数(160位)

      Warning: SHA-1 is cryptographically weak. Use only for checksums.

      警告: SHA-1 较弱。仅用于校验。

      Returns:
      hash function | 哈希函数
    • sha256

      public static MessageDigestHashFunction sha256()
      Creates a SHA-256 function (256-bit) 创建 SHA-256 函数(256位)
      Returns:
      hash function | 哈希函数
    • sha512

      public static MessageDigestHashFunction sha512()
      Creates a SHA-512 function (512-bit) 创建 SHA-512 函数(512位)
      Returns:
      hash function | 哈希函数
    • sha3_256

      public static MessageDigestHashFunction sha3_256()
      Creates a SHA3-256 function (256-bit) 创建 SHA3-256 函数(256位)
      Returns:
      hash function | 哈希函数
    • sha3_512

      public static MessageDigestHashFunction sha3_512()
      Creates a SHA3-512 function (512-bit) 创建 SHA3-512 函数(512位)
      Returns:
      hash function | 哈希函数
    • sha384

      public static MessageDigestHashFunction sha384()
      Creates a SHA-384 function (384-bit) 创建 SHA-384 函数(384位)
      Returns:
      hash function | 哈希函数
    • create

      public static MessageDigestHashFunction create(String algorithm, int bits)
      Creates a hash function for a custom algorithm 为自定义算法创建哈希函数
      Parameters:
      algorithm - algorithm name (must be supported by JDK) | 算法名称(必须被JDK支持)
      bits - output bits | 输出位数
      Returns:
      hash function | 哈希函数
      Throws:
      OpenHashException - if algorithm is not supported | 如果算法不支持
    • 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 | 哈希码