Class HashCode
java.lang.Object
cloud.opencode.base.hash.HashCode
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 TypeMethodDescriptionabstract byte[]asBytes()Converts to byte array 转换为字节数组abstract intasInt()Converts to int (uses lower 32 bits) 转换为int(使用低32位)abstract longasLong()Converts to long (uses lower 64 bits) 转换为long(使用低64位)abstract intbits()Gets the number of bits in this hash 获取此哈希的位数booleanstatic HashCodefromBase64(String base64) Creates a HashCode from a Base64 string 从Base64字符串创建HashCodestatic HashCodefromBytes(byte[] bytes) Creates a HashCode from a byte array 从字节数组创建HashCodestatic HashCodeCreates a HashCode from a hexadecimal string 从十六进制字符串创建HashCodestatic HashCodefromInt(int hash) Creates a HashCode from an int value 从int值创建HashCodestatic HashCodefromLong(long hash) Creates a HashCode from a long value 从long值创建HashCodeinthashCode()abstract longConverts to long with padding for 32-bit hashes 32位哈希填充后转换为longtoBase64()Converts to URL-safe Base64 string (no padding) 转换为URL安全的Base64字符串(无填充)toHex()Converts to hexadecimal string 转换为十六进制字符串toString()abstract intwriteBytesTo(byte[] dest, int offset) Writes hash bytes to a destination array 将哈希字节写入目标数组
-
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
Converts to hexadecimal string 转换为十六进制字符串- Returns:
- hexadecimal string | 十六进制字符串
-
toBase64
Converts to URL-safe Base64 string (no padding) 转换为URL安全的Base64字符串(无填充)- Returns:
- Base64 string | Base64字符串
-
fromInt
Creates a HashCode from an int value 从int值创建HashCode- Parameters:
hash- int hash value | int哈希值- Returns:
- HashCode instance | HashCode实例
-
fromLong
Creates a HashCode from a long value 从long值创建HashCode- Parameters:
hash- long hash value | long哈希值- Returns:
- HashCode instance | HashCode实例
-
fromBytes
Creates a HashCode from a byte array 从字节数组创建HashCode- Parameters:
bytes- byte array | 字节数组- Returns:
- HashCode instance | HashCode实例
-
fromHex
-
fromBase64
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
-
hashCode
-
toString
-