Package com.privatechain.crypto
Class HashUtil
java.lang.Object
com.privatechain.crypto.HashUtil
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
sha256(String)/sha256(byte[])— NIST SHA-256sha3_256(String)/sha3_256(byte[])— Keccak / NIST SHA-3-256doubleSha256(String)/doubleSha256(byte[])— SHA-256(SHA-256(x)), the double-hash used in Bitcoin-style block and transaction IDs
Thread safety
All methods create freshMessageDigest 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 Summary
Modifier and TypeMethodDescriptionstatic StringdoubleSha256(byte[] data) Computes SHA-256(SHA-256(data)) on a byte array.static StringdoubleSha256(String input) Computes SHA-256(SHA-256(input)) on a UTF-8–encoded string.static Stringsha256(byte[] data) Computes the SHA-256 hash of a byte array.static StringComputes the SHA-256 hash of a UTF-8–encoded string.static Stringsha3_256(byte[] data) Computes the SHA-3-256 hash of a byte array.static StringComputes the SHA-3-256 hash of a UTF-8–encoded string.
-
Method Details
-
sha256
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
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
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
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
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
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
-