Class HashCode

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

public abstract class HashCode extends Object
Hash computation result wrapper 哈希计算结果封装

Represents the result of a hash computation. Provides methods to access the hash value in different formats including int, long, byte array, and hexadecimal string.

表示哈希计算的结果。提供以不同格式访问哈希值的方法, 包括int、long、字节数组和十六进制字符串。

Features | 主要功能:

  • Multiple output formats - 多种输出格式
  • Factory methods for creation - 创建工厂方法
  • Immutable and thread-safe - 不可变且线程安全

Usage Examples | 使用示例:

HashCode hash = OpenHash.murmur3_128().hashUtf8("Hello");
int intValue = hash.asInt();
long longValue = hash.asLong();
String hex = hash.toHex();
byte[] bytes = hash.asBytes();

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)

Performance | 性能特性:

  • Time complexity: O(n) where n = byte array length - O(n), n为字节数组长度
  • Space complexity: O(n) for hash bytes - 哈希字节 O(n)
Since:
JDK 25, opencode-base-hash V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract byte[]
    Converts to byte array 转换为字节数组
    abstract int
    Converts to int (uses lower 32 bits) 转换为int(使用低32位)
    abstract long
    Converts to long (uses lower 64 bits) 转换为long(使用低64位)
    abstract int
    Gets the number of bits in this hash 获取此哈希的位数
    boolean
     
    static HashCode
    Creates a HashCode from a Base64 string 从Base64字符串创建HashCode
    static HashCode
    fromBytes(byte[] bytes)
    Creates a HashCode from a byte array 从字节数组创建HashCode
    static HashCode
    Creates a HashCode from a hexadecimal string 从十六进制字符串创建HashCode
    static HashCode
    fromInt(int hash)
    Creates a HashCode from an int value 从int值创建HashCode
    static HashCode
    fromLong(long hash)
    Creates a HashCode from a long value 从long值创建HashCode
    int
     
    abstract long
    Converts to long with padding for 32-bit hashes 32位哈希填充后转换为long
    Converts to URL-safe Base64 string (no padding) 转换为URL安全的Base64字符串(无填充)
    Converts to hexadecimal string 转换为十六进制字符串
     
    abstract int
    writeBytesTo(byte[] dest, int offset)
    Writes hash bytes to a destination array 将哈希字节写入目标数组

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • bits

      public abstract int bits()
      Gets the number of bits in this hash 获取此哈希的位数
      Returns:
      number of bits | 位数
    • asInt

      public abstract int asInt()
      Converts to int (uses lower 32 bits) 转换为int(使用低32位)
      Returns:
      int value | int值
    • asLong

      public abstract long asLong()
      Converts to long (uses lower 64 bits) 转换为long(使用低64位)
      Returns:
      long value | long值
      Throws:
      IllegalStateException - if hash is less than 64 bits | 如果哈希少于64位
    • padToLong

      public abstract long padToLong()
      Converts to long with padding for 32-bit hashes 32位哈希填充后转换为long
      Returns:
      long value | long值
    • asBytes

      public abstract byte[] asBytes()
      Converts to byte array 转换为字节数组
      Returns:
      byte array (defensive copy) | 字节数组(防御性复制)
    • writeBytesTo

      public abstract int writeBytesTo(byte[] dest, int offset)
      Writes hash bytes to a destination array 将哈希字节写入目标数组
      Parameters:
      dest - destination array | 目标数组
      offset - starting offset | 起始偏移
      Returns:
      number of bytes written | 写入的字节数
    • toHex

      public String toHex()
      Converts to hexadecimal string 转换为十六进制字符串
      Returns:
      hexadecimal string | 十六进制字符串
    • toBase64

      public String toBase64()
      Converts to URL-safe Base64 string (no padding) 转换为URL安全的Base64字符串(无填充)
      Returns:
      Base64 string | Base64字符串
    • fromInt

      public static HashCode fromInt(int hash)
      Creates a HashCode from an int value 从int值创建HashCode
      Parameters:
      hash - int hash value | int哈希值
      Returns:
      HashCode instance | HashCode实例
    • fromLong

      public static HashCode fromLong(long hash)
      Creates a HashCode from a long value 从long值创建HashCode
      Parameters:
      hash - long hash value | long哈希值
      Returns:
      HashCode instance | HashCode实例
    • fromBytes

      public static HashCode fromBytes(byte[] bytes)
      Creates a HashCode from a byte array 从字节数组创建HashCode
      Parameters:
      bytes - byte array | 字节数组
      Returns:
      HashCode instance | HashCode实例
    • fromHex

      public static HashCode fromHex(String hex)
      Creates a HashCode from a hexadecimal string 从十六进制字符串创建HashCode
      Parameters:
      hex - hexadecimal string | 十六进制字符串
      Returns:
      HashCode instance | HashCode实例
    • fromBase64

      public static HashCode fromBase64(String base64)
      Creates a HashCode from a Base64 string 从Base64字符串创建HashCode
      Parameters:
      base64 - URL-safe Base64 string (no padding) | URL安全的Base64字符串(无填充)
      Returns:
      HashCode instance | HashCode实例
      Throws:
      OpenHashException - if base64 string is invalid | 如果Base64字符串无效
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object