Class Ecc
- All Implemented Interfaces:
AutoCloseable
Generate EC keys, create ECDSA signatures, verify signatures, and compute shared secrets.
Chilkat.Ecc is the Chilkat class for direct elliptic-curve
cryptographic operations. It can generate EC private keys, sign hashes or
data using ECDSA, verify ECDSA signatures, sign through ECDSA certificates,
and compute encoded shared secrets from compatible EC key pairs. It is used
when an application needs explicit control over elliptic-curve keys,
signatures, signature encoding, or key-agreement output.
Generate EC private keys
Create elliptic-curve private keys for supported curves and use the resulting key with Chilkat signing, verification, or key-agreement APIs.
ECDSA signing
Sign hashes or input data using an EC private key, producing ECDSA signatures in the configured output format.
ECDSA verification
Verify ECDSA signatures using the matching EC public key and the original hash or data.
Certificate-based signing
Sign with an ECDSA certificate when the certificate has access to its associated private key.
Shared-secret calculation
Compute an encoded shared secret from compatible EC private and public keys for key-agreement workflows.
Signature format control
Use AsnFormat to control whether ECDSA signatures are
encoded as ASN.1/DER or as raw r || s values.
Common pattern:
Generate or load an EC key, choose the required signature format with
AsnFormat, then sign or verify using the method that matches the
input form: hash, data, key, or certificate. For shared-secret workflows,
compute the ECDH-style secret and pass the result through the key derivation
or encoding step required by the protocol.
For an extended overview, see Ecc Class Overview.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidgenKey(String curveName, Prng prng, PrivateKey privKey)Generates a new elliptic-curve private key and stores it inprivKey.voidgenKey2(String curveName, String encodedK, String encoding, PrivateKey privKey)Constructs an elliptic-curve private key inprivKeyfrom a caller-supplied private scalar.booleanControls the binary layout of ECDSA signatures created by this object.voidsetAsnFormat(boolean value)Sets theAsnFormatproperty; seegetAsnFormat().sharedSecretENC(PrivateKey privKey, PublicKey pubKey, String encoding)Computes an elliptic-curve Diffie-Hellman shared secret from the private key inprivKeyand the peer's public key inpubKey.Hashes the bytes currently contained inbdDataand creates an ECDSA signature with the EC private key inprivKey.signBdUsingCert(BinData bdData, String hashAlg, String encoding, Cert cert)Hashes the bytes inbdDataand creates an ECDSA signature using the private key associated with the certificate incert.signHashENC(String encodedHash, String encoding, PrivateKey privkey, Prng prng)Creates an ECDSA signature over a digest that has already been computed by the application.signHashUsingCert(String encodedHash, String encoding, Cert cert)Creates an ECDSA signature over a digest that has already been computed.intHashes the bytes inbdDatausing the algorithm named byhashAlgand verifies the ECDSA signature inencodedSigwith the EC public key inpubkey.intverifyHashENC(String encodedHash, String encodedSig, String encoding, PublicKey pubkey)Verifies an ECDSA signature over a digest that was computed by the application.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
Ecc
public Ecc()Creates a newEccobject.
-
-
Method Details
-
getAsnFormat
public boolean getAsnFormat()Controls the binary layout of ECDSA signatures created by this object. The default istrue.Value Signature layout
trueASN.1 DER: aSEQUENCEcontaining the two INTEGER valuesrands. Its encoded length can vary because DER INTEGER values may require leading sign-padding bytes.falseRaw format: fixed-width, unsigned big-endianrfollowed immediately by fixed-widths, commonly writtenr || s.Signing versus verification: This property controls generated signatures. Chilkat verification methods automatically recognize both ASN.1 DER and raw
r || ssignatures. Protocol compatibility: Neither format is universally interchangeable. Use the layout required by the receiving protocol or file format.- Returns:
- the value of the
AsnFormatproperty
-
setAsnFormat
public void setAsnFormat(boolean value)Sets theAsnFormatproperty; seegetAsnFormat().- Parameters:
value- the new value
-
genKey
Generates a new elliptic-curve private key and stores it inprivKey.curveNameselects the named curve, andprngsupplies the cryptographically secure random data used to choose the private scalar. Returnstrueon success andfalseon failure.Supported curve names are:
Curve name Notes
secp256r1Also known as P-256 andprime256v1.secp384r1Also known as P-384.secp521r1Also known as P-521.secp256k1Widely associated with Bitcoin and related systems.secp192r1,secp224r1Older, smaller SEC curves retained for compatibility.brainpoolP160r1,brainpoolP192r1,brainpoolP224r1,brainpoolP256r1,brainpoolP320r1,brainpoolP384r1,brainpoolP512r1Brainpool prime-field curves.Choose the curve required by the protocol: The curve name determines the key size, signature size, and interoperability. P-256, P-384, and P-521 are broadly standardized;
secp256k1is commonly required by Bitcoin-related systems. Older and smaller curves should generally be used only for compatibility with an existing format or peer. Random generation matters: The security of the private key depends on the randomness supplied byprng. Use a properly initialized cryptographicPrng.- Parameters:
curveName- thecurveNameargument (notnull)prng- theprngargument (notnull)privKey- theprivKeyargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
genKey2
Constructs an elliptic-curve private key inprivKeyfrom a caller-supplied private scalar.curveNameselects the named curve,encodedKcontains the scalar, andencodingnames its representation, such ashex,base64, ordecimal. Returnstrueon success andfalseon failure.Supported curve names are:
Curve name Notes
secp256r1Also known as P-256 andprime256v1.secp384r1Also known as P-384.secp521r1Also known as P-521.secp256k1Widely associated with Bitcoin and related systems.secp192r1,secp224r1Older, smaller SEC curves retained for compatibility.brainpoolP160r1,brainpoolP192r1,brainpoolP224r1,brainpoolP256r1,brainpoolP320r1,brainpoolP384r1,brainpoolP512r1Brainpool prime-field curves.encodedKis the private key value: Despite the historical argument name,encodedKis the long-lived private scalar used to construct the key. It is not the temporary ECDSA signing nonce also commonly denoted by k. The value must be valid for the selected curve, unpredictable when used as a real key, and kept secret. Typical use: Use this method to reproduce a known key for a test vector or to import a scalar supplied by another system. Useto create a new random private key.genKey(String, Prng, PrivateKey)- Parameters:
curveName- thecurveNameargument (notnull)encodedK- theencodedKargument (notnull)encoding- theencodingargument (notnull)privKey- theprivKeyargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
signBd
public String signBd(BinData bdData, String hashAlg, String encoding, PrivateKey privKey, Prng prng)Hashes the bytes currently contained inbdDataand creates an ECDSA signature with the EC private key inprivKey.hashAlgselects the hash algorithm and may besha256,sha384, orsha512.encodingselects the textual encoding of the returned signature, such ashexorbase64.prngsupplies the random source used during ECDSA signature generation.The signature's binary layout is controlled by
. The contents of thegetAsnFormat()BinDataobject are read but not modified.Sign the exact bytes: Verification succeeds only when the verifier hashes the same byte sequence with the same hash algorithm and uses the public key corresponding to
privKey. Protect the signing nonce: Use a cryptographically securePrng. Reusing or predicting an ECDSA nonce can expose the private key.- Parameters:
bdData- thebdDataargument (notnull)hashAlg- thehashAlgargument (notnull)encoding- theencodingargument (notnull)privKey- theprivKeyargument (notnull)prng- theprngargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
signBdUsingCert
Hashes the bytes inbdDataand creates an ECDSA signature using the private key associated with the certificate incert.hashAlgselectssha256,sha384, orsha512;encodingselects the textual encoding of the returned signature.The certificate must contain an EC public key and must provide access to its corresponding private key. The private key may be embedded with the certificate or supplied through the platform or key-storage mechanism from which the certificate was loaded. Signature layout is controlled by
.getAsnFormat()Certificate versus key: The certificate identifies the public key and may describe the key owner, but signing requires actual access to the associated private key. A public-only certificate cannot sign.
- Parameters:
bdData- thebdDataargument (notnull)hashAlg- thehashAlgargument (notnull)encoding- theencodingargument (notnull)cert- thecertargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
signHashENC
Creates an ECDSA signature over a digest that has already been computed by the application.encodedHashcontains the digest encoded using the binary encoding named byencoding, such ashexorbase64.privkeysupplies the EC private key, andprngsupplies the random source used during signature generation.The returned signature uses the same textual encoding named by
encoding. Its binary layout is controlled by: ASN.1 DER whengetAsnFormat()true, or rawr || swhenfalse.No hashing is performed:
encodedHashmust be the exact digest required by the protocol. The verifier must use the same digest bytes and the matching public key. ECDSA nonce safety: Every signature requires a secret per-signature value. It must be generated securely and must never be reused with the same private key, because nonce reuse or predictability can reveal the private key. Supply a properly initialized cryptographicPrng.- Parameters:
encodedHash- theencodedHashargument (notnull)encoding- theencodingargument (notnull)privkey- theprivkeyargument (notnull)prng- theprngargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
signHashUsingCert
Creates an ECDSA signature over a digest that has already been computed.encodedHashcontains the digest encoded using the binary encoding named byencoding.certsupplies a certificate whose public key is EC and whose associated private key is accessible for signing.The returned signature uses the same textual encoding named by
encoding. Its binary layout is controlled by.getAsnFormat()No hashing is performed:
encodedHashmust contain the final digest bytes expected by the receiving protocol. Usewhen Chilkat should hash the source data before signing. Private-key requirement: A certificate containing only a public key is not sufficient. The associated EC private key must be present and usable.signBdUsingCert(BinData, String, String, Cert)- Parameters:
encodedHash- theencodedHashargument (notnull)encoding- theencodingargument (notnull)cert- thecertargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
verifyBd
public int verifyBd(BinData bdData, String hashAlg, String encodedSig, String encoding, PublicKey pubkey)Hashes the bytes inbdDatausing the algorithm named byhashAlgand verifies the ECDSA signature inencodedSigwith the EC public key inpubkey.encodingidentifies the textual encoding of the signature, such ashexorbase64. Supported hash algorithms aresha256,sha384, andsha512.Chilkat automatically recognizes ASN.1 DER and raw
r || ssignature layouts.Return Meaning
1Valid signature.0Invalid signature.-1Operational failure; examineLastErrorText.Exact-data requirement: A valid signature becomes invalid if any signed byte changes or if a different hash algorithm is selected.
- Parameters:
bdData- thebdDataargument (notnull)hashAlg- thehashAlgargument (notnull)encodedSig- theencodedSigargument (notnull)encoding- theencodingargument (notnull)pubkey- thepubkeyargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
verifyHashENC
Verifies an ECDSA signature over a digest that was computed by the application.encodedHashis the encoded digest,encodedSigis the encoded signature,encodingnames the binary encoding used by both strings, andpubkeysupplies the matching EC public key.Chilkat automatically recognizes both supported ECDSA signature layouts: ASN.1 DER and fixed-width raw
r || s. Theproperty therefore does not need to match the signature being verified.getAsnFormat()Return Meaning
1The signature is valid for the supplied digest and public key.0The method ran successfully, but the signature is not valid.-1The verification operation could not be completed because of malformed input, an unsuitable key, an unsupported encoding, or another error. ExamineLastErrorText.Digest-level verification: This method does not hash the original message. It verifies only the digest bytes supplied in
encodedHash.- Parameters:
encodedHash- theencodedHashargument (notnull)encodedSig- theencodedSigargument (notnull)encoding- theencodingargument (notnull)pubkey- thepubkeyargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-