Class HashUtil

java.lang.Object
com.privatechain.crypto.HashUtil

public final class HashUtil extends Object
Utility class providing cryptographic hash functions used throughout the blockchain.

All methods return lowercase hexadecimal strings. BouncyCastle is registered lazily on first use so that the JVM does not pay the registration cost when the class is loaded but no hash is computed (FR-CRYPTO-01 – FR-CRYPTO-03).

Supported algorithms

Thread safety

All methods create fresh MessageDigest instances for each call so they are safe to use from multiple threads simultaneously.

 String blockHash = HashUtil.sha256("index|prevHash|merkleRoot|timestamp");
 String txId      = HashUtil.doubleSha256(rawBytes);
 
Since:
1.0.0
See Also:
  • Method Details

    • sha256

      public static String sha256(String input)
      Computes the SHA-256 hash of a UTF-8–encoded string.
      Parameters:
      input - the string to hash (non-null)
      Returns:
      lowercase 64-character hex string
      Throws:
      NullPointerException - if input is null
    • sha256

      public static String sha256(byte[] data)
      Computes the SHA-256 hash of a byte array.
      Parameters:
      data - the data to hash (non-null)
      Returns:
      lowercase 64-character hex string
      Throws:
      NullPointerException - if data is null
    • sha3_256

      public static String sha3_256(String input)
      Computes the SHA-3-256 hash of a UTF-8–encoded string.

      SHA-3-256 (Keccak) is used in Ethereum-compatible address derivation and as an alternative to SHA-256 in hash-proof generation.

      Parameters:
      input - the string to hash (non-null)
      Returns:
      lowercase 64-character hex string
      Throws:
      NullPointerException - if input is null
    • sha3_256

      public static String sha3_256(byte[] data)
      Computes the SHA-3-256 hash of a byte array.
      Parameters:
      data - the data to hash (non-null)
      Returns:
      lowercase 64-character hex string
      Throws:
      NullPointerException - if data is null
    • doubleSha256

      public static String doubleSha256(String input)
      Computes SHA-256(SHA-256(input)) on a UTF-8–encoded string.

      Double-hashing is used in Bitcoin-style transaction and block IDs to provide additional collision resistance.

      Parameters:
      input - the string to hash (non-null)
      Returns:
      lowercase 64-character hex string
      Throws:
      NullPointerException - if input is null
    • doubleSha256

      public static String doubleSha256(byte[] data)
      Computes SHA-256(SHA-256(data)) on a byte array.
      Parameters:
      data - the data to hash (non-null)
      Returns:
      lowercase 64-character hex string
      Throws:
      NullPointerException - if data is null