Class OpenHash

java.lang.Object
cloud.opencode.base.hash.OpenHash

public final class OpenHash extends Object
Hash utility facade class 哈希工具门面类

Provides a unified entry point for all hash functions and data structures. This is the main API for hash operations in the library.

为所有哈希函数和数据结构提供统一的入口点。 这是库中哈希操作的主要API。

Features | 主要功能:

  • Non-cryptographic hashes: MurmurHash3, xxHash, FNV-1a, CRC32, SipHash, Adler32 - 非加密哈希
  • Cryptographic hashes: MD5, SHA-1, SHA-256, SHA-384, SHA-512, SHA-3 - 加密哈希
  • HMAC: HMAC-MD5, HMAC-SHA1, HMAC-SHA256, HMAC-SHA384, HMAC-SHA512 - HMAC消息认证
  • Consistent hash ring - 一致性哈希环
  • Bloom filter - 布隆过滤器
  • Counting bloom filter - 计数布隆过滤器
  • SimHash for text similarity - SimHash用于文本相似度
  • HashCodes combiner for efficient hashCode() - HashCodes组合器用于高效hashCode()

Usage Examples | 使用示例:

// Hash a string
HashCode hash = OpenHash.murmur3_128().hashUtf8("Hello World");

// Consistent hash ring
ConsistentHash<String> ring = OpenHash.<String>consistentHash()
    .addNode("server1", "192.168.1.1")
    .build();

// Bloom filter
BloomFilter<String> filter = OpenHash.<String>bloomFilter()
    .funnel(Funnel.STRING_FUNNEL)
    .expectedInsertions(1_000_000)
    .build();

// SimHash
SimHash simHash = OpenHash.simHash().nGram(3).build();

Security | 安全性:

  • Thread-safe: Yes (factory class) - 线程安全: 是(工厂类)

Performance | 性能特性:

  • Time complexity: O(n) where n = input size - O(n), n为输入大小
  • Space complexity: O(1) for hash computation - 哈希计算 O(1)
Since:
JDK 25, opencode-base-hash V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • murmur3_32

      public static HashFunction murmur3_32()
      MurmurHash3 32-bit MurmurHash3 32位
      Returns:
      hash function | 哈希函数
    • murmur3_32

      public static HashFunction murmur3_32(int seed)
      MurmurHash3 32-bit with seed 带种子的MurmurHash3 32位
      Parameters:
      seed - seed value | 种子值
      Returns:
      hash function | 哈希函数
    • murmur3_128

      public static HashFunction murmur3_128()
      MurmurHash3 128-bit MurmurHash3 128位
      Returns:
      hash function | 哈希函数
    • murmur3_128

      public static HashFunction murmur3_128(int seed)
      MurmurHash3 128-bit with seed 带种子的MurmurHash3 128位
      Parameters:
      seed - seed value | 种子值
      Returns:
      hash function | 哈希函数
    • xxHash64

      public static HashFunction xxHash64()
      xxHash 64-bit xxHash 64位
      Returns:
      hash function | 哈希函数
    • xxHash64

      public static HashFunction xxHash64(long seed)
      xxHash 64-bit with seed 带种子的xxHash 64位
      Parameters:
      seed - seed value | 种子值
      Returns:
      hash function | 哈希函数
    • fnv1a_32

      public static HashFunction fnv1a_32()
      FNV-1a 32-bit FNV-1a 32位
      Returns:
      hash function | 哈希函数
    • fnv1a_64

      public static HashFunction fnv1a_64()
      FNV-1a 64-bit FNV-1a 64位
      Returns:
      hash function | 哈希函数
    • crc32

      public static HashFunction crc32()
      CRC32 CRC32
      Returns:
      hash function | 哈希函数
    • crc32c

      public static HashFunction crc32c()
      CRC32C (Castagnoli) CRC32C (Castagnoli)
      Returns:
      hash function | 哈希函数
    • adler32

      public static HashFunction adler32()
      Adler-32 checksum Adler-32 校验和
      Returns:
      hash function | 哈希函数
    • sipHash24

      public static HashFunction sipHash24()
      SipHash-2-4 with default key 默认密钥的 SipHash-2-4
      Returns:
      hash function | 哈希函数
    • sipHash24

      public static HashFunction sipHash24(long k0, long k1)
      SipHash-2-4 with specified key 指定密钥的 SipHash-2-4
      Parameters:
      k0 - first key half | 密钥前半部分
      k1 - second key half | 密钥后半部分
      Returns:
      hash function | 哈希函数
    • md5

      public static HashFunction md5()
      MD5 (for checksums only, not secure) MD5(仅用于校验,不安全)
      Returns:
      hash function | 哈希函数
    • sha1

      public static HashFunction sha1()
      SHA-1 (for checksums only, not secure) SHA-1(仅用于校验,不安全)
      Returns:
      hash function | 哈希函数
    • sha256

      public static HashFunction sha256()
      SHA-256 SHA-256
      Returns:
      hash function | 哈希函数
    • sha512

      public static HashFunction sha512()
      SHA-512 SHA-512
      Returns:
      hash function | 哈希函数
    • sha3_256

      public static HashFunction sha3_256()
      SHA3-256 SHA3-256
      Returns:
      hash function | 哈希函数
    • sha384

      public static HashFunction sha384()
      SHA-384 SHA-384
      Returns:
      hash function | 哈希函数
    • sha3_512

      public static HashFunction sha3_512()
      SHA3-512 SHA3-512
      Returns:
      hash function | 哈希函数
    • hmacMd5

      public static HashFunction hmacMd5(byte[] key)
      HMAC-MD5 HMAC-MD5
      Parameters:
      key - secret key bytes | 密钥字节
      Returns:
      hash function | 哈希函数
    • hmacSha1

      public static HashFunction hmacSha1(byte[] key)
      HMAC-SHA1 HMAC-SHA1
      Parameters:
      key - secret key bytes | 密钥字节
      Returns:
      hash function | 哈希函数
    • hmacSha256

      public static HashFunction hmacSha256(byte[] key)
      HMAC-SHA256 HMAC-SHA256
      Parameters:
      key - secret key bytes | 密钥字节
      Returns:
      hash function | 哈希函数
    • hmacSha384

      public static HashFunction hmacSha384(byte[] key)
      HMAC-SHA384 HMAC-SHA384
      Parameters:
      key - secret key bytes | 密钥字节
      Returns:
      hash function | 哈希函数
    • hmacSha512

      public static HashFunction hmacSha512(byte[] key)
      HMAC-SHA512 HMAC-SHA512
      Parameters:
      key - secret key bytes | 密钥字节
      Returns:
      hash function | 哈希函数
    • hash

      public static HashCode hash(CharSequence input, Charset charset, HashFunction function)
      Hashes a string 哈希字符串
      Parameters:
      input - input string | 输入字符串
      charset - character set | 字符集
      function - hash function | 哈希函数
      Returns:
      hash code | 哈希码
    • hash

      public static HashCode hash(byte[] input, HashFunction function)
      Hashes a byte array 哈希字节数组
      Parameters:
      input - input bytes | 输入字节
      function - hash function | 哈希函数
      Returns:
      hash code | 哈希码
    • combineOrdered

      public static HashCode combineOrdered(HashCode... hashCodes)
      Combines hash codes in order 按顺序组合哈希码
      Parameters:
      hashCodes - hash codes to combine | 要组合的哈希码
      Returns:
      combined hash code | 组合的哈希码
    • combineUnordered

      public static HashCode combineUnordered(HashCode... hashCodes)
      Combines hash codes unordered (XOR) 无序组合哈希码(XOR)
      Parameters:
      hashCodes - hash codes to combine | 要组合的哈希码
      Returns:
      combined hash code | 组合的哈希码
    • consistentHash

      public static <T> ConsistentHashBuilder<T> consistentHash()
      Creates a consistent hash ring builder 创建一致性哈希环构建器
      Type Parameters:
      T - node data type | 节点数据类型
      Returns:
      builder | 构建器
    • bloomFilter

      public static <T> BloomFilterBuilder<T> bloomFilter()
      Creates a bloom filter builder 创建布隆过滤器构建器
      Type Parameters:
      T - element type | 元素类型
      Returns:
      builder | 构建器
    • bloomFilter

      public static <T> BloomFilterBuilder<T> bloomFilter(Funnel<? super T> funnel)
      Creates a bloom filter builder with funnel 使用funnel创建布隆过滤器构建器
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      funnel - element funnel | 元素funnel
      Returns:
      builder | 构建器
    • countingBloomFilter

      public static <T> CountingBloomFilter.Builder<T> countingBloomFilter()
      Creates a counting bloom filter builder 创建计数布隆过滤器构建器
      Type Parameters:
      T - element type | 元素类型
      Returns:
      builder | 构建器
    • countingBloomFilter

      public static <T> CountingBloomFilter.Builder<T> countingBloomFilter(Funnel<? super T> funnel)
      Creates a counting bloom filter builder with funnel 使用funnel创建计数布隆过滤器构建器
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      funnel - element funnel | 元素funnel
      Returns:
      builder | 构建器
    • simHash

      public static SimHashBuilder simHash()
      Creates a SimHash builder 创建SimHash构建器
      Returns:
      builder | 构建器