Delphi DLL Requires Chilkat v9.5.0.85+
Delphi DLL
ECDSA Sign and Verify Data using Different Hash Algorithms
See more ECC Examples
Demonstrates how to create ECDSA signatures on data using different hash algorithms.Chilkat Delphi DLL Downloads
var
success: Boolean;
privKey: HCkPrivateKey;
bd: HCkBinData;
ecdsa: HCkEcc;
prng: HCkPrng;
sig: PWideChar;
pubKey: HCkPublicKey;
ecc2: HCkEcc;
result: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First load an ECDSA private key to be used for signing.
privKey := CkPrivateKey_Create();
success := CkPrivateKey_LoadEncryptedPemFile(privKey,'qa_data/ecc/secp256r1-key-pkcs8-secret.pem','secret');
if (success = False) then
begin
Memo1.Lines.Add(CkPrivateKey__lastErrorText(privKey));
Exit;
end;
// Load some data to be signed.
bd := CkBinData_Create();
success := CkBinData_LoadFile(bd,'qa_data/hamlet.xml');
if (success = False) then
begin
Memo1.Lines.Add('Failed to load file to be hashed.');
Exit;
end;
ecdsa := CkEcc_Create();
prng := CkPrng_Create();
// Sign the sha256 hash of the data. Return the ECDSA signature in the base64 encoding.
Memo1.Lines.Add('ECDSA signing the sha256 hash of the data...');
sig := CkEcc__signBd(ecdsa,bd,'sha256','base64',privKey,prng);
Memo1.Lines.Add('sig = ' + sig);
// Verify the signature against the original data.
// (We must use the same hash algorithm that was used when signing.)
// Load the public key that corresponds to the private key used for signing.
pubKey := CkPublicKey_Create();
success := CkPublicKey_LoadFromFile(pubKey,'qa_data/ecc/secp256r1-pub.pem');
if (success = False) then
begin
Memo1.Lines.Add(CkPublicKey__lastErrorText(pubKey));
Exit;
end;
ecc2 := CkEcc_Create();
result := CkEcc_VerifyBd(ecc2,bd,'sha256',sig,'base64',pubKey);
if (result <> 1) then
begin
Memo1.Lines.Add(CkEcc__lastErrorText(ecc2));
Exit;
end;
Memo1.Lines.Add('Verified!');
// ----------------------------------------------------------------------------------------
// Let's do the same thing, but with sha384 hashing...
Memo1.Lines.Add('--------------------------------------------');
Memo1.Lines.Add('ECDSA signing the sha384 hash of the data...');
sig := CkEcc__signBd(ecdsa,bd,'sha384','base64',privKey,prng);
Memo1.Lines.Add('sig = ' + sig);
result := CkEcc_VerifyBd(ecc2,bd,'sha384',sig,'base64',pubKey);
if (result <> 1) then
begin
Memo1.Lines.Add(CkEcc__lastErrorText(ecc2));
Exit;
end;
Memo1.Lines.Add('Verified!');
CkPrivateKey_Dispose(privKey);
CkBinData_Dispose(bd);
CkEcc_Dispose(ecdsa);
CkPrng_Dispose(prng);
CkPublicKey_Dispose(pubKey);
CkEcc_Dispose(ecc2);