Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
RSA Sign Using Private Key from .pfx/.p12 to Base64 Signature
See more RSA Examples
Demonstrates how to RSA sign something using a private key loaded from a .pfx/.p12. The RSA signature is returned in Base64 encoded format.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.Pfx,
Chilkat.PrivateKey,
Chilkat.Rsa;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rsa: TRsa;
pfx: TPfx;
privKey: TPrivateKey;
strData: string;
base64Sig: string;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rsa := TRsa.Create;
// Load the .pfx/.p12
pfx := TPfx.Create;
success := pfx.LoadPfxFile('qa_data/pfx/myKey.p12','myPassword');
if (success = False) then
begin
WriteLn(pfx.LastErrorText);
Exit;
end;
// Get the default private key.
privKey := TPrivateKey.Create;
success := pfx.PrivateKeyAt(0,privKey);
if (success = False) then
begin
WriteLn(pfx.LastErrorText);
Exit;
end;
// Import the private key into the RSA component:
success := rsa.UsePrivateKey(privKey);
if (success = False) then
begin
WriteLn(rsa.LastErrorText);
Exit;
end;
// Get the signature in base64
rsa.EncodingMode := 'base64';
strData := 'This is the string to be signed.';
// Sign the string using the sha256 hash algorithm.
// Other valid choices are "sha384", "sha512", "sha-1", "md2" and "md5".
base64Sig := rsa.SignStringENC(strData,'sha256');
WriteLn(base64Sig);
WriteLn('Success!');
rsa.Free;
pfx.Free;
privKey.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.