Package com.privatechain.crypto
Class ECDSASignatureUtil
java.lang.Object
com.privatechain.crypto.ECDSASignatureUtil
Utility class for ECDSA signing and signature verification using the
secp256k1 elliptic curve
and the SHA-256withECDSA algorithm.
All operations use BouncyCastle as the JCA provider because secp256k1 is not available in the standard JDK provider on all JVM versions (FR-CRYPTO-04, FR-CRYPTO-05).
Usage
ECKeyPair keyPair = KeyPairGenerator.generateECKeyPair();
// Signing
byte[] data = tx.toSignableBytes();
byte[] signature = ECDSASignatureUtil.sign(data, keyPair.getPrivateKey());
// Verification
boolean valid = ECDSASignatureUtil.verify(data, signature, keyPair.getPublicKey());
Thread safety
All methods create freshSignature instances per call,
so they are safe to call concurrently from multiple threads.- Since:
- 1.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]Convenience overload that accepts anECKeyPairdirectly.static byte[]sign(byte[] data, PrivateKey privateKey) Signs a byte array with the given ECDSA private key using SHA-256withECDSA.static booleanConvenience overload that accepts anECKeyPairfor verification.static booleanVerifies a DER-encoded ECDSA signature against raw data and a public key.
-
Method Details
-
sign
Signs a byte array with the given ECDSA private key using SHA-256withECDSA.The returned DER-encoded signature may be passed to
verify(byte[], byte[], PublicKey)to confirm the signer's identity.- Parameters:
data- the raw bytes to sign (non-null; may be empty but typically non-empty)privateKey- the signer's secp256k1 private key (non-null)- Returns:
- DER-encoded ECDSA signature bytes (typically 70–72 bytes)
- Throws:
NullPointerException- if data or privateKey is nullIllegalStateException- if the BouncyCastle provider is unavailable or the key is rejected by the JCA engine
-
sign
Convenience overload that accepts anECKeyPairdirectly.- Parameters:
data- the raw bytes to sign (non-null)keyPair- the key pair whose private key will be used (non-null)- Returns:
- DER-encoded ECDSA signature bytes
- Throws:
NullPointerException- if data or keyPair is nullIllegalStateException- if signing fails
-
verify
Verifies a DER-encoded ECDSA signature against raw data and a public key.This method must be called before any transaction enters the mempool (NFR-SEC-02). It returns
false— rather than throwing — on any verification failure so that callers can treat invalid signatures as normal validation failures rather than exceptional errors.- Parameters:
data- the original data that was signed (non-null)signature- the DER-encoded ECDSA signature to verify (non-null)publicKey- the signer's secp256k1 public key (non-null)- Returns:
trueif the signature is valid;falseotherwise- Throws:
NullPointerException- if any argument is nullIllegalStateException- if the BouncyCastle provider is unavailable
-
verify
Convenience overload that accepts anECKeyPairfor verification.- Parameters:
data- the original data that was signed (non-null)signature- the DER-encoded ECDSA signature to verify (non-null)keyPair- the key pair whose public key will be used (non-null)- Returns:
trueif the signature is valid- Throws:
NullPointerException- if any argument is null
-