Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v9.5.0.85+

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 Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.BinData,
  Chilkat.PrivateKey,
  Chilkat.Prng,
  Chilkat.Ecc,
  Chilkat.PublicKey;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  privKey: TPrivateKey;
  bd: TBinData;
  ecdsa: TEcc;
  prng: TPrng;
  sig: string;
  pubKey: TPublicKey;
  ecc2: TEcc;
  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 := TPrivateKey.Create;
  success := privKey.LoadEncryptedPemFile('qa_data/ecc/secp256r1-key-pkcs8-secret.pem','secret');
  if (success = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  //  Load some data to be signed.
  bd := TBinData.Create;
  success := bd.LoadFile('qa_data/hamlet.xml');
  if (success = False) then
    begin
      WriteLn('Failed to load file to be hashed.');
      Exit;
    end;

  ecdsa := TEcc.Create;
  prng := TPrng.Create;

  //  Sign the sha256 hash of the data.  Return the ECDSA signature in the base64 encoding.
  WriteLn('ECDSA signing the sha256 hash of the data...');
  sig := ecdsa.SignBd(bd,'sha256','base64',privKey,prng);
  WriteLn('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 := TPublicKey.Create;
  success := pubKey.LoadFromFile('qa_data/ecc/secp256r1-pub.pem');
  if (success = False) then
    begin
      WriteLn(pubKey.LastErrorText);
      Exit;
    end;

  ecc2 := TEcc.Create;
  result := ecc2.VerifyBd(bd,'sha256',sig,'base64',pubKey);
  if (result <> 1) then
    begin
      WriteLn(ecc2.LastErrorText);
      Exit;
    end;

  WriteLn('Verified!');

  //  ----------------------------------------------------------------------------------------
  //  Let's do the same thing, but with sha384 hashing...

  WriteLn('--------------------------------------------');
  WriteLn('ECDSA signing the sha384 hash of the data...');

  sig := ecdsa.SignBd(bd,'sha384','base64',privKey,prng);
  WriteLn('sig = ' + sig);

  result := ecc2.VerifyBd(bd,'sha384',sig,'base64',pubKey);
  if (result <> 1) then
    begin
      WriteLn(ecc2.LastErrorText);
      Exit;
    end;

  WriteLn('Verified!');


  privKey.Free;
  bd.Free;
  ecdsa.Free;
  prng.Free;
  pubKey.Free;
  ecc2.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.