Class OpenDigest
java.lang.Object
cloud.opencode.base.crypto.OpenDigest
Message digest facade for computing hash digests - Provides convenient API for various hash algorithms
消息摘要门面类 - 为各种哈希算法提供便捷的 API
Features | 主要功能:
- SHA-2 family (SHA-256, SHA-384, SHA-512) - SHA-2 系列(SHA-256、SHA-384、SHA-512)
- SHA-3 family (SHA3-256, SHA3-512) - SHA-3 系列(SHA3-256、SHA3-512)
- SM3, BLAKE2b, BLAKE3 support - SM3、BLAKE2b、BLAKE3 支持
- File and stream digesting - 文件和流摘要
- Streaming (incremental) computation - 流式(增量)计算
Usage Examples | 使用示例:
// One-shot hash
String hex = OpenDigest.sha256().digestHex("Hello");
// Streaming hash
String hash = OpenDigest.sha512()
.update("part1")
.update("part2")
.doFinalHex();
// File hash
String fileHash = OpenDigest.sha256().digestFileHex(Path.of("file.txt"));
Security | 安全性:
- Thread-safe: No - 线程安全: 否
- Null-safe: Yes - 空值安全: 是
Performance | 性能特性:
- Time complexity: O(n) - 时间复杂度: O(n),n为数据长度
- Space complexity: O(1) - 空间复杂度: O(1)
- Since:
- JDK 25, opencode-base-crypto V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic OpenDigestblake2b(int digestLength) Create BLAKE2b digester (requires Bouncy Castle) 创建 BLAKE2b 摘要器(需要 Bouncy Castle)static OpenDigestblake3()Create BLAKE3 digester (requires Bouncy Castle) 创建 BLAKE3 摘要器(需要 Bouncy Castle)byte[]digest(byte[] data) Compute digest of byte array 计算字节数组的摘要byte[]digest(InputStream input) Compute digest of input stream 计算输入流摘要byte[]Compute digest of string (UTF-8) 计算字符串的摘要(UTF-8)digestBase64(byte[] data) Compute digest and return as Base64 string 计算摘要并返回 Base64 字符串digestBase64(String data) Compute digest of string and return as Base64 string 计算字符串摘要并返回 Base64 字符串byte[]digestFile(Path file) Compute digest of file 计算文件摘要digestFileHex(Path file) Compute digest of file and return as hex string 计算文件摘要并返回十六进制字符串digestHex(byte[] data) Compute digest and return as hex string 计算摘要并返回十六进制字符串digestHex(InputStream input) Compute digest of input stream and return as hex string 计算输入流摘要并返回十六进制字符串Compute digest of string and return as hex string 计算字符串摘要并返回十六进制字符串byte[]doFinal()Complete computation and return digest 完成计算并返回摘要Complete computation and return as Base64 string 完成计算并返回 Base64 字符串Complete computation and return as hex string 完成计算并返回十六进制字符串Get algorithm name 获取算法名称intGet digest length in bytes 获取摘要长度(字节)static OpenDigestof(DigestAlgorithm algorithm) Create digester by algorithm enum 根据算法枚举创建摘要器reset()Reset digest state 重置摘要状态static OpenDigestsha256()Create SHA-256 digester 创建 SHA-256 摘要器static OpenDigestsha3_256()Create SHA3-256 digester 创建 SHA3-256 摘要器static OpenDigestsha3_512()Create SHA3-512 digester 创建 SHA3-512 摘要器static OpenDigestsha384()Create SHA-384 digester 创建 SHA-384 摘要器static OpenDigestsha512()Create SHA-512 digester 创建 SHA-512 摘要器static OpenDigestsm3()Create SM3 digester (requires Bouncy Castle) 创建 SM3 摘要器(需要 Bouncy Castle)update(byte[] data) Update digest with data 更新摘要数据update(byte[] data, int offset, int length) Update digest with partial data 更新摘要部分数据Update digest with string (UTF-8) 更新摘要字符串(UTF-8)update(ByteBuffer buffer) Update digest with ByteBuffer 更新摘要 ByteBuffer
-
Method Details
-
sha256
Create SHA-256 digester 创建 SHA-256 摘要器- Returns:
- OpenDigest instance
-
sha384
Create SHA-384 digester 创建 SHA-384 摘要器- Returns:
- OpenDigest instance
-
sha512
Create SHA-512 digester 创建 SHA-512 摘要器- Returns:
- OpenDigest instance
-
sha3_256
Create SHA3-256 digester 创建 SHA3-256 摘要器- Returns:
- OpenDigest instance
-
sha3_512
Create SHA3-512 digester 创建 SHA3-512 摘要器- Returns:
- OpenDigest instance
-
sm3
Create SM3 digester (requires Bouncy Castle) 创建 SM3 摘要器(需要 Bouncy Castle)- Returns:
- OpenDigest instance
-
blake2b
Create BLAKE2b digester (requires Bouncy Castle) 创建 BLAKE2b 摘要器(需要 Bouncy Castle)- Parameters:
digestLength- digest length in bytes- Returns:
- OpenDigest instance
-
blake3
Create BLAKE3 digester (requires Bouncy Castle) 创建 BLAKE3 摘要器(需要 Bouncy Castle)- Returns:
- OpenDigest instance
-
of
Create digester by algorithm enum 根据算法枚举创建摘要器- Parameters:
algorithm- digest algorithm- Returns:
- OpenDigest instance
-
digest
public byte[] digest(byte[] data) Compute digest of byte array 计算字节数组的摘要- Parameters:
data- input data- Returns:
- digest bytes
-
digest
Compute digest of string (UTF-8) 计算字符串的摘要(UTF-8)- Parameters:
data- input string- Returns:
- digest bytes
-
digestHex
Compute digest and return as hex string 计算摘要并返回十六进制字符串- Parameters:
data- input data- Returns:
- hex string
-
digestHex
-
digestBase64
Compute digest and return as Base64 string 计算摘要并返回 Base64 字符串- Parameters:
data- input data- Returns:
- Base64 string
-
digestBase64
-
digestFile
Compute digest of file 计算文件摘要- Parameters:
file- file path- Returns:
- digest bytes
-
digestFileHex
-
digest
Compute digest of input stream 计算输入流摘要- Parameters:
input- input stream- Returns:
- digest bytes
-
digestHex
Compute digest of input stream and return as hex string 计算输入流摘要并返回十六进制字符串- Parameters:
input- input stream- Returns:
- hex string
-
update
Update digest with data 更新摘要数据- Parameters:
data- data to add- Returns:
- this instance for chaining
-
update
Update digest with partial data 更新摘要部分数据- Parameters:
data- data arrayoffset- start offsetlength- number of bytes- Returns:
- this instance for chaining
-
update
Update digest with string (UTF-8) 更新摘要字符串(UTF-8)- Parameters:
data- string to add- Returns:
- this instance for chaining
-
update
Update digest with ByteBuffer 更新摘要 ByteBuffer- Parameters:
buffer- ByteBuffer to add- Returns:
- this instance for chaining
-
doFinal
public byte[] doFinal()Complete computation and return digest 完成计算并返回摘要- Returns:
- digest bytes
-
doFinalHex
Complete computation and return as hex string 完成计算并返回十六进制字符串- Returns:
- hex string
-
doFinalBase64
Complete computation and return as Base64 string 完成计算并返回 Base64 字符串- Returns:
- Base64 string
-
reset
-
getAlgorithm
-
getDigestLength
public int getDigestLength()Get digest length in bytes 获取摘要长度(字节)- Returns:
- digest length
-