Interface HashFunction

All Known Implementing Classes:
Blake2Hash, Blake3Hash, Sha2Hash, Sha3Hash, Sm3Hash

public interface HashFunction
Hash function interface for cryptographic hash operations - Provides unified API for various hash algorithms 哈希函数接口 - 为各种哈希算法提供统一的 API

Features | 主要功能:

  • Byte array and string hashing - 字节数组和字符串哈希
  • Hex and Base64 output encoding - 十六进制和 Base64 输出编码
  • Algorithm name and digest length access - 算法名称和摘要长度访问

Usage Examples | 使用示例:

HashFunction sha256 = Sha2Hash.sha256();
byte[] digest = sha256.hash("data");
String hex = sha256.hashHex("data");

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Partial - 空值安全: 部分

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
    Get the hash algorithm name 获取哈希算法名称
    int
    Get the digest length in bytes 获取摘要长度(字节数)
    byte[]
    hash(byte[] data)
    Compute hash of byte array 计算字节数组的哈希值
    byte[]
    hash(String data)
    Compute hash of string (UTF-8 encoded) 计算字符串的哈希值(使用 UTF-8 编码)
    hashBase64(byte[] data)
    Compute hash and return as Base64 string 计算哈希值并返回 Base64 字符串
    hashHex(byte[] data)
    Compute hash and return as hexadecimal string 计算哈希值并返回十六进制字符串
    Compute hash of string and return as hexadecimal string 计算字符串哈希值并返回十六进制字符串
  • Method Details

    • hash

      byte[] hash(byte[] data)
      Compute hash of byte array 计算字节数组的哈希值
      Parameters:
      data - input data to hash
      Returns:
      hash digest as byte array
      Throws:
      NullPointerException - if data is null
    • hash

      byte[] hash(String data)
      Compute hash of string (UTF-8 encoded) 计算字符串的哈希值(使用 UTF-8 编码)
      Parameters:
      data - input string to hash
      Returns:
      hash digest as byte array
      Throws:
      NullPointerException - if data is null
    • hashHex

      String hashHex(byte[] data)
      Compute hash and return as hexadecimal string 计算哈希值并返回十六进制字符串
      Parameters:
      data - input data to hash
      Returns:
      hash digest as lowercase hex string
      Throws:
      NullPointerException - if data is null
    • hashHex

      String hashHex(String data)
      Compute hash of string and return as hexadecimal string 计算字符串哈希值并返回十六进制字符串
      Parameters:
      data - input string to hash
      Returns:
      hash digest as lowercase hex string
      Throws:
      NullPointerException - if data is null
    • hashBase64

      String hashBase64(byte[] data)
      Compute hash and return as Base64 string 计算哈希值并返回 Base64 字符串
      Parameters:
      data - input data to hash
      Returns:
      hash digest as Base64 string
      Throws:
      NullPointerException - if data is null
    • getDigestLength

      int getDigestLength()
      Get the digest length in bytes 获取摘要长度(字节数)
      Returns:
      digest length in bytes
    • getAlgorithm

      String getAlgorithm()
      Get the hash algorithm name 获取哈希算法名称
      Returns:
      algorithm name