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 TypeMethodDescriptionintbits()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 HashCodeComputes hash of a file 计算文件的哈希default HashCodehashInputStream(InputStream inputStream) 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值的哈希<T> HashCodehashObject(T instance, Funnel<? super T> funnel) Computes hash of an object using a Funnel 使用Funnel计算对象的哈希default HashCodehashString(CharSequence input, Charset charset) Computes hash of a string with specified charset 计算指定字符集字符串的哈希default HashCodehashUtf8(CharSequence input) Computes hash of a UTF-8 string 计算UTF-8字符串的哈希name()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
Creates a Hasher with expected input size hint 创建带预期输入大小提示的Hasher- Parameters:
expectedInputSize- expected input size in bytes | 预期输入大小(字节)- Returns:
- a new Hasher | 新的Hasher
-
hashBytes
Computes hash of a byte array 计算字节数组的哈希- Parameters:
input- input bytes | 输入字节- Returns:
- hash code | 哈希码
-
hashBytes
Computes hash of a byte array portion 计算字节数组部分的哈希- Parameters:
input- input bytes | 输入字节offset- starting offset | 起始偏移length- number of bytes to hash | 要哈希的字节数- Returns:
- hash code | 哈希码
-
hashString
Computes hash of a string with specified charset 计算指定字符集字符串的哈希- Parameters:
input- input string | 输入字符串charset- character set | 字符集- Returns:
- hash code | 哈希码
-
hashUtf8
Computes hash of a UTF-8 string 计算UTF-8字符串的哈希- Parameters:
input- input string | 输入字符串- Returns:
- hash code | 哈希码
-
hashInt
Computes hash of an int value 计算int值的哈希- Parameters:
input- int value | int值- Returns:
- hash code | 哈希码
-
hashLong
Computes hash of a long value 计算long值的哈希- Parameters:
input- long value | long值- Returns:
- hash code | 哈希码
-
hashObject
-
bits
int bits()Gets the number of bits in the hash output 获取哈希输出的位数- Returns:
- number of bits | 位数
-
name
-
hashInputStream
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
-