Interface HashFunction

All Known Implementing Classes:
AbstractHashFunction, Adler32HashFunction, Crc32HashFunction, Fnv1aHashFunction, HmacHashFunction, MessageDigestHashFunction, Murmur3HashFunction, SipHashFunction, XxHashFunction

public interface HashFunction
Hash function interface 哈希函数接口

Defines the contract for hash functions that can compute hash codes for various input types including bytes, strings, and objects.

定义哈希函数的契约,可以计算各种输入类型的哈希码,包括字节、字符串和对象。

Features | 主要功能:

  • Byte array hashing - 字节数组哈希
  • String hashing with charset - 带字符集的字符串哈希
  • Primitive type hashing - 原始类型哈希
  • Object hashing via Funnel - 通过Funnel的对象哈希
  • Streaming hash via Hasher - 通过Hasher的流式哈希

Usage Examples | 使用示例:

HashFunction murmur = OpenHash.murmur3_128();
HashCode hash = murmur.hashUtf8("Hello World");
System.out.println(hash.toHex());

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是(无状态)
Since:
JDK 25, opencode-base-hash V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Gets the number of bits in the hash output 获取哈希输出的位数
    hashBytes(byte[] input)
    Computes hash of a byte array 计算字节数组的哈希
    hashBytes(byte[] input, int offset, int length)
    Computes hash of a byte array portion 计算字节数组部分的哈希
    default HashCode
    hashFile(Path path)
    Computes hash of a file 计算文件的哈希
    default HashCode
    Computes hash of an InputStream 计算输入流的哈希
    hashInt(int input)
    Computes hash of an int value 计算int值的哈希
    hashLong(long input)
    Computes hash of a long value 计算long值的哈希
    hashObject(T instance, Funnel<? super T> funnel)
    Computes hash of an object using a Funnel 使用Funnel计算对象的哈希
    default HashCode
    hashString(CharSequence input, Charset charset)
    Computes hash of a string with specified charset 计算指定字符集字符串的哈希
    default HashCode
    Computes hash of a UTF-8 string 计算UTF-8字符串的哈希
    Gets the algorithm name 获取算法名称
    Creates a new Hasher instance for streaming hash computation 创建新的Hasher实例用于流式哈希计算
    newHasher(int expectedInputSize)
    Creates a Hasher with expected input size hint 创建带预期输入大小提示的Hasher
  • Method Details

    • newHasher

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

      Hasher newHasher(int expectedInputSize)
      Creates a Hasher with expected input size hint 创建带预期输入大小提示的Hasher
      Parameters:
      expectedInputSize - expected input size in bytes | 预期输入大小(字节)
      Returns:
      a new Hasher | 新的Hasher
    • hashBytes

      HashCode hashBytes(byte[] input)
      Computes hash of a byte array 计算字节数组的哈希
      Parameters:
      input - input bytes | 输入字节
      Returns:
      hash code | 哈希码
    • hashBytes

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

      default HashCode hashString(CharSequence input, Charset charset)
      Computes hash of a string with specified charset 计算指定字符集字符串的哈希
      Parameters:
      input - input string | 输入字符串
      charset - character set | 字符集
      Returns:
      hash code | 哈希码
    • hashUtf8

      default HashCode hashUtf8(CharSequence input)
      Computes hash of a UTF-8 string 计算UTF-8字符串的哈希
      Parameters:
      input - input string | 输入字符串
      Returns:
      hash code | 哈希码
    • hashInt

      HashCode hashInt(int input)
      Computes hash of an int value 计算int值的哈希
      Parameters:
      input - int value | int值
      Returns:
      hash code | 哈希码
    • hashLong

      HashCode hashLong(long input)
      Computes hash of a long value 计算long值的哈希
      Parameters:
      input - long value | long值
      Returns:
      hash code | 哈希码
    • hashObject

      <T> HashCode hashObject(T instance, Funnel<? super T> funnel)
      Computes hash of an object using a Funnel 使用Funnel计算对象的哈希
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      instance - object instance | 对象实例
      funnel - serialization funnel | 序列化通道
      Returns:
      hash code | 哈希码
    • bits

      int bits()
      Gets the number of bits in the hash output 获取哈希输出的位数
      Returns:
      number of bits | 位数
    • name

      String name()
      Gets the algorithm name 获取算法名称
      Returns:
      algorithm name | 算法名称
    • hashInputStream

      default HashCode hashInputStream(InputStream inputStream)
      Computes hash of an InputStream 计算输入流的哈希

      Reads the stream in 8KB chunks without loading the entire content into memory. The stream is NOT closed by this method.

      以8KB块读取流,不会将全部内容加载到内存。此方法不关闭流。

      Parameters:
      inputStream - input stream | 输入流
      Returns:
      hash code | 哈希码
    • hashFile

      default HashCode hashFile(Path path)
      Computes hash of a file 计算文件的哈希

      Reads the file in 8KB chunks without loading the entire content into memory.

      以8KB块读取文件,不会将全部内容加载到内存。

      Parameters:
      path - file path | 文件路径
      Returns:
      hash code | 哈希码