Class OpenDigest

java.lang.Object
cloud.opencode.base.crypto.OpenDigest

public final class OpenDigest extends Object
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 Type
    Method
    Description
    static OpenDigest
    blake2b(int digestLength)
    Create BLAKE2b digester (requires Bouncy Castle) 创建 BLAKE2b 摘要器(需要 Bouncy Castle)
    static OpenDigest
    Create BLAKE3 digester (requires Bouncy Castle) 创建 BLAKE3 摘要器(需要 Bouncy Castle)
    byte[]
    digest(byte[] data)
    Compute digest of byte array 计算字节数组的摘要
    byte[]
    Compute digest of input stream 计算输入流摘要
    byte[]
    digest(String data)
    Compute digest of string (UTF-8) 计算字符串的摘要(UTF-8)
    digestBase64(byte[] data)
    Compute digest and return as Base64 string 计算摘要并返回 Base64 字符串
    Compute digest of string and return as Base64 string 计算字符串摘要并返回 Base64 字符串
    byte[]
    Compute digest of file 计算文件摘要
    Compute digest of file and return as hex string 计算文件摘要并返回十六进制字符串
    digestHex(byte[] data)
    Compute digest and return as hex string 计算摘要并返回十六进制字符串
    Compute digest of input stream and return as hex string 计算输入流摘要并返回十六进制字符串
    Compute digest of string and return as hex string 计算字符串摘要并返回十六进制字符串
    byte[]
    Complete computation and return digest 完成计算并返回摘要
    Complete computation and return as Base64 string 完成计算并返回 Base64 字符串
    Complete computation and return as hex string 完成计算并返回十六进制字符串
    Get algorithm name 获取算法名称
    int
    Get digest length in bytes 获取摘要长度(字节)
    static OpenDigest
    of(DigestAlgorithm algorithm)
    Create digester by algorithm enum 根据算法枚举创建摘要器
    Reset digest state 重置摘要状态
    static OpenDigest
    Create SHA-256 digester 创建 SHA-256 摘要器
    static OpenDigest
    Create SHA3-256 digester 创建 SHA3-256 摘要器
    static OpenDigest
    Create SHA3-512 digester 创建 SHA3-512 摘要器
    static OpenDigest
    Create SHA-384 digester 创建 SHA-384 摘要器
    static OpenDigest
    Create SHA-512 digester 创建 SHA-512 摘要器
    static OpenDigest
    sm3()
    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(String data)
    Update digest with string (UTF-8) 更新摘要字符串(UTF-8)
    Update digest with ByteBuffer 更新摘要 ByteBuffer

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • sha256

      public static OpenDigest sha256()
      Create SHA-256 digester 创建 SHA-256 摘要器
      Returns:
      OpenDigest instance
    • sha384

      public static OpenDigest sha384()
      Create SHA-384 digester 创建 SHA-384 摘要器
      Returns:
      OpenDigest instance
    • sha512

      public static OpenDigest sha512()
      Create SHA-512 digester 创建 SHA-512 摘要器
      Returns:
      OpenDigest instance
    • sha3_256

      public static OpenDigest sha3_256()
      Create SHA3-256 digester 创建 SHA3-256 摘要器
      Returns:
      OpenDigest instance
    • sha3_512

      public static OpenDigest sha3_512()
      Create SHA3-512 digester 创建 SHA3-512 摘要器
      Returns:
      OpenDigest instance
    • sm3

      public static OpenDigest sm3()
      Create SM3 digester (requires Bouncy Castle) 创建 SM3 摘要器(需要 Bouncy Castle)
      Returns:
      OpenDigest instance
    • blake2b

      public static OpenDigest blake2b(int digestLength)
      Create BLAKE2b digester (requires Bouncy Castle) 创建 BLAKE2b 摘要器(需要 Bouncy Castle)
      Parameters:
      digestLength - digest length in bytes
      Returns:
      OpenDigest instance
    • blake3

      public static OpenDigest blake3()
      Create BLAKE3 digester (requires Bouncy Castle) 创建 BLAKE3 摘要器(需要 Bouncy Castle)
      Returns:
      OpenDigest instance
    • of

      public static OpenDigest of(DigestAlgorithm algorithm)
      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

      public byte[] digest(String data)
      Compute digest of string (UTF-8) 计算字符串的摘要(UTF-8)
      Parameters:
      data - input string
      Returns:
      digest bytes
    • digestHex

      public String digestHex(byte[] data)
      Compute digest and return as hex string 计算摘要并返回十六进制字符串
      Parameters:
      data - input data
      Returns:
      hex string
    • digestHex

      public String digestHex(String data)
      Compute digest of string and return as hex string 计算字符串摘要并返回十六进制字符串
      Parameters:
      data - input string
      Returns:
      hex string
    • digestBase64

      public String digestBase64(byte[] data)
      Compute digest and return as Base64 string 计算摘要并返回 Base64 字符串
      Parameters:
      data - input data
      Returns:
      Base64 string
    • digestBase64

      public String digestBase64(String data)
      Compute digest of string and return as Base64 string 计算字符串摘要并返回 Base64 字符串
      Parameters:
      data - input string
      Returns:
      Base64 string
    • digestFile

      public byte[] digestFile(Path file)
      Compute digest of file 计算文件摘要
      Parameters:
      file - file path
      Returns:
      digest bytes
    • digestFileHex

      public String digestFileHex(Path file)
      Compute digest of file and return as hex string 计算文件摘要并返回十六进制字符串
      Parameters:
      file - file path
      Returns:
      hex string
    • digest

      public byte[] digest(InputStream input)
      Compute digest of input stream 计算输入流摘要
      Parameters:
      input - input stream
      Returns:
      digest bytes
    • digestHex

      public String digestHex(InputStream input)
      Compute digest of input stream and return as hex string 计算输入流摘要并返回十六进制字符串
      Parameters:
      input - input stream
      Returns:
      hex string
    • update

      public OpenDigest update(byte[] data)
      Update digest with data 更新摘要数据
      Parameters:
      data - data to add
      Returns:
      this instance for chaining
    • update

      public OpenDigest update(byte[] data, int offset, int length)
      Update digest with partial data 更新摘要部分数据
      Parameters:
      data - data array
      offset - start offset
      length - number of bytes
      Returns:
      this instance for chaining
    • update

      public OpenDigest update(String data)
      Update digest with string (UTF-8) 更新摘要字符串(UTF-8)
      Parameters:
      data - string to add
      Returns:
      this instance for chaining
    • update

      public OpenDigest update(ByteBuffer buffer)
      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

      public String doFinalHex()
      Complete computation and return as hex string 完成计算并返回十六进制字符串
      Returns:
      hex string
    • doFinalBase64

      public String doFinalBase64()
      Complete computation and return as Base64 string 完成计算并返回 Base64 字符串
      Returns:
      Base64 string
    • reset

      public OpenDigest reset()
      Reset digest state 重置摘要状态
      Returns:
      this instance for chaining
    • getAlgorithm

      public String getAlgorithm()
      Get algorithm name 获取算法名称
      Returns:
      algorithm name
    • getDigestLength

      public int getDigestLength()
      Get digest length in bytes 获取摘要长度(字节)
      Returns:
      digest length