Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Verify XML Digital Signature
See more XML Digital Signatures Examples
Verifies XML signatures in an XML file.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.XmlDSig,
Chilkat.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sbXml: TStringBuilder;
dsig: TXmlDSig;
i: Integer;
bVerifyReferenceDigests: Boolean;
bVerified: Boolean;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
sbXml := TStringBuilder.Create;
success := sbXml.LoadFile('qa_data/xml_dsig_verify/csioz_sample.xml','utf-8');
if (success <> True) then
begin
WriteLn('Failed to load XML file.');
Exit;
end;
dsig := TXmlDSig.Create;
// First load the XML containing the signatures to be verified.
success := dsig.LoadSignatureSb(sbXml);
if (success <> True) then
begin
WriteLn(dsig.LastErrorText);
Exit;
end;
// It's possible that an XML document can contain multiple signatures.
// Each can be verified as follows:
i := 0;
while i < dsig.NumSignatures do
begin
// Select the Nth signature by setting the Selector property.
dsig.Selector := i;
// The bVerifyReferenceDigests argument determines if we want
// to also verify each reference digest. If set to False,
// then only the SignedInfo part of the Signature is verified.
bVerifyReferenceDigests := True;
bVerified := dsig.VerifySignature(bVerifyReferenceDigests);
WriteLn('Signature ' + i + 1 + ' verified = ' + bVerified);
i := i + 1;
end;
sbXml.Free;
dsig.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.