Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Verfies an RSA Signature
See more Apple Keychain Examples
Verifies an RSA signature against the original data.Chilkat Pascal (Lazarus/Delphi) Downloads
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.Rsa,
Chilkat.PublicKey;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
bd: TBinData;
i: Integer;
bdSig: TBinData;
pubKey: TPublicKey;
rsa: TRsa;
begin
success := False;
// The following data was signed by the following example:
// RSA Sign using a Private Key on a USB Token or Smartcard
bd := TBinData.Create;
for i := 0 to 100 do
begin
bd.AppendEncoded('000102030405060708090A0B0C0D0E0F','hex');
end;
// Load the signature
bdSig := TBinData.Create;
success := bdSig.LoadFile('rsaSignatures/test1.sig');
if (success = False) then
begin
WriteLn('Failed to load the RSA signature');
Exit;
end;
// Get the public key to be used for signature verification.
pubKey := TPublicKey.Create;
success := pubKey.LoadFromFile('rsaKeys/chilkat-rsa-2048.pem');
if (success = False) then
begin
WriteLn(pubKey.LastErrorText);
Exit;
end;
rsa := TRsa.Create;
success := rsa.UsePublicKey(pubKey);
if (success = False) then
begin
WriteLn(rsa.LastErrorText);
Exit;
end;
// Verify the hash of the data against the signature.
// We pass in the original data. Internally, the hash is generated
// and used to validate the signature.
// Validating the RSA signature means two things:
// (1) the original data is exactly what was signed, and
// (2) it was signed by the owner of the RSA private key.
success := rsa.VerifyBd(bd,'sha256',bdSig);
if (success = False) then
begin
WriteLn(rsa.LastErrorText);
WriteLn('Signature invalid.');
end
else
begin
WriteLn('Signature valid.');
end;
bd.Free;
bdSig.Free;
pubKey.Free;
rsa.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.