Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Create PEM-encoded PKCS#7 Detached Signature

See more Digital Signatures Examples

Demonstrates how to create a PKCS7 PEM-encoded object containing a detached signature.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.StringBuilder,
  Chilkat.Cert,
  Chilkat.Crypt2;

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

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  crypt: TCrypt2;
  textToSign: string;
  sigBase64: string;
  sb: TStringBuilder;
  crlf: Boolean;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  cert := TCert.Create;

  //  Load the cert and private key.
  success := cert.LoadPfxFile('qa_data/pfx/myCertAndKey.p12','password');
  if (success <> True) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  crypt := TCrypt2.Create;

  success := crypt.SetSigningCert(cert);
  if (success <> True) then
    begin
      WriteLn(crypt.LastErrorText);
      Exit;
    end;

  //  Use SHA-256
  crypt.HashAlgorithm := 'sha256';

  //  Hash the utf-8 byte representation of the string
  crypt.Charset := 'utf-8';

  //  Return the result in base64
  crypt.EncodingMode := 'base64Mime';

  //  Sign some text to create a detached signature (i.e. a signature that does not include the signed data)
  textToSign := 'This is the text to be hashed and signed.';
  sigBase64 := crypt.SignStringENC(textToSign);
  if (crypt.LastMethodSuccess <> True) then
    begin
      WriteLn(crypt.LastErrorText);
      Exit;
    end;

  WriteLn(sigBase64);

  //  The result:

  //  MIIWbgYJKoZIhvcNAQcCoIIWXzCCFlsCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg
  //  ghMXMIIFuzCCA6OgAwIBAgIIVwoRl0LE48wwDQYJKoZIhvcNAQELBQAwazELMAkGA1UEBhMCSVQx
  //  ...
  //  ...

  //  If we want it in PEM format with just LF line-endings:
  sb := TStringBuilder.Create;
  //  Just LF line endings, not CRLF.
  crlf := False;
  sb.AppendLine('-----BEGIN PKCS7-----',crlf);
  sb.Append(sigBase64);
  sb.AppendLine('-----END PKCS7-----',crlf);
  sb.ToLF();

  //  Save to a file.
  sb.WriteFile('c:/temp/qa_output/sig.pem','utf-8',False);

  //  Examine..
  WriteLn(sb.GetAsString());

  //  Result is:

  //  -----BEGIN PKCS7-----
  //  MIIWbgYJKoZIhvcNAQcCoIIWXzCCFlsCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg
  //  ghMXMIIFuzCCA6OgAwIBAgIIVwoRl0LE48wwDQYJKoZIhvcNAQELBQAwazELMAkGA1UEBhMCSVQx
  //  DjAMBgNVBAcMBU1pbGFuMSMwIQYDVQQKDBpBY3RhbGlzIFMucC5BLi8wMzM1ODUyMDk2NzEnMCUG
  //  A1UEAwweQWN0YWxpcyBBdXRoZW50aWNhdGlvbiBSb290IENBMB4XDTExMDkyMjExMjIwMloXDTMw
  //  MDkyMjExMjIwMlowazELMAkGA1UEBhMCSVQxDjAMBgNVBAcMBU1pbGFuMSMwIQYDVQQKDBpBY3Rh
  //  bGlzIFMucC5BLi8wMzM1ODUyMDk2NzEnMCUGA1UEAwweQWN0YWxpcyBBdXRoZW50aWNhdGlvbiBS
  //  b290IENBMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAp8bEpSmkLO/lGMWwUKNvUTuf
  //  ClrJwkg4CsIcoBh/kbWHuUA/3R1oHwiD1S0eiKD4j1aPbZkCkpAW1V8IbInX4ay8IMKx4INRimlN
  //  AJZaby/ARH6jDuSRzVju3PvHHkVH3Se5CAGfpiEd9UEtL0z9KK3giq0itFZljoZUj5NDKd45Rnij
  //  MCO6zfB9E1fAXdKDa0hMxKufgFpbOr3JpyI/gCczWw63igxdBzcIy2zSekciRDXFzMwujt0q7bd9
  //  ...
  //  ...
  //  -----END PKCS7-----


  cert.Free;
  crypt.Free;
  sb.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.