Class SipHashFunction
java.lang.Object
cloud.opencode.base.hash.function.AbstractHashFunction
cloud.opencode.base.hash.function.SipHashFunction
- All Implemented Interfaces:
HashFunction
SipHash-2-4 hash function implementation
SipHash-2-4 哈希函数实现
A pure Java implementation of SipHash-2-4, a fast short-input PRF designed by Jean-Philippe Aumasson and Daniel J. Bernstein. SipHash produces a 64-bit output and is optimized for short messages, making it ideal for hash table lookups and hash-flooding protection.
SipHash-2-4 的纯 Java 实现,这是由 Jean-Philippe Aumasson 和 Daniel J. Bernstein 设计的快速短输入 PRF。SipHash 产生 64 位输出,针对短消息优化, 非常适合哈希表查找和哈希洪泛防护。
Features | 主要功能:
- 64-bit keyed hash output - 64位密钥哈希输出
- 2 compression rounds, 4 finalization rounds - 2轮压缩,4轮终结
- Configurable 128-bit key (k0, k1) - 可配置128位密钥(k0, k1)
- Hash-flooding resistant - 抗哈希洪泛攻击
- Streaming and one-shot hashing - 支持流式和一次性哈希
Usage Examples | 使用示例:
// Default key (k0=0, k1=0)
// 默认密钥(k0=0, k1=0)
HashCode hash = SipHashFunction.sipHash24().hashUtf8("Hello World");
// Custom key
// 自定义密钥
HashCode hash2 = SipHashFunction.sipHash24(0x0706050403020100L, 0x0f0e0d0c0b0a0908L)
.hashUtf8("Hello World");
// Streaming API
// 流式API
Hasher hasher = SipHashFunction.sipHash24().newHasher();
hasher.putInt(42).putLong(123L);
HashCode hash3 = hasher.hash();
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Key not exposed in toString() - toString()不暴露密钥
- Not for cryptographic use - 不用于加密用途
Performance | 性能特性:
- Time complexity: O(n) where n = input size - O(n), n为输入大小
- Space complexity: O(1) for hash state - 哈希状态 O(1)
- 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 计算字节数组部分的哈希Creates a new Hasher instance for streaming hash computation 创建新的Hasher实例用于流式哈希计算static SipHashFunctionCreates a SipHash-2-4 function with default key (k0=0, k1=0) 使用默认密钥(k0=0, k1=0)创建SipHash-2-4函数static SipHashFunctionsipHash24(long k0, long k1) Creates a SipHash-2-4 function with specified 128-bit key 使用指定的128位密钥创建SipHash-2-4函数toString()Returns string representation without exposing key values 返回字符串表示,不暴露密钥值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
-
sipHash24
Creates a SipHash-2-4 function with default key (k0=0, k1=0) 使用默认密钥(k0=0, k1=0)创建SipHash-2-4函数- Returns:
- hash function | 哈希函数
-
sipHash24
Creates a SipHash-2-4 function with specified 128-bit key 使用指定的128位密钥创建SipHash-2-4函数- Parameters:
k0- first 64 bits of the key | 密钥的前64位k1- last 64 bits of the key | 密钥的后64位- Returns:
- hash function | 哈希函数
-
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
Returns string representation without exposing key values 返回字符串表示,不暴露密钥值- Overrides:
toStringin classAbstractHashFunction- Returns:
- string representation | 字符串表示
-