Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Create PKCS7 Signed File (.p7m)
See more Encryption Examples
Demonstrates how to sign a file to create a .p7m that contains both the file contents and the signature.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.JsonObject,
Chilkat.Cert,
Chilkat.CertStore,
Chilkat.Crypt2;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
crypt: TCrypt2;
certStore: TCertStore;
jsonCN: TJsonObject;
cert: TCert;
inFile: string;
outFile: string;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
crypt := TCrypt2.Create;
certStore := TCertStore.Create;
// Load a PFX file into a certificate store object.
success := certStore.LoadPfxFile('myPfx.pfx','pfxPassword');
if (success <> True) then
begin
WriteLn(certStore.LastErrorText);
Exit;
end;
// Get the certificate by subject common name.
// This should be the cert within the PFX that also
// has a private key (also stored within the PFX).
jsonCN := TJsonObject.Create;
jsonCN.UpdateString('CN','myCert');
cert := TCert.Create;
success := certStore.FindCert(jsonCN,cert);
if (success = False) then
begin
WriteLn(certStore.LastErrorText);
Exit;
end;
// Tell the crypt object to use the certificate for signing:
success := crypt.SetSigningCert(cert);
// Sign a file, producing a .p7m as output.
// The input file is unchanged, the test.p7m contains the
// contents of the input file and the signature.
inFile := 'test.txt';
outFile := 'testp7m';
success := crypt.CreateP7M(inFile,outFile);
if (success <> True) then
begin
WriteLn(crypt.LastErrorText);
Exit;
end;
WriteLn('Success!');
crypt.Free;
certStore.Free;
jsonCN.Free;
cert.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.