Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get a Certificate's Key Size
See more Certificates Examples
Demonstrates how to get the RSA key size of a certificate (for example, 1024-bit, 2048-bit, etc.)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.Xml,
Chilkat.StringBuilder,
Chilkat.BinData,
Chilkat.Cert,
Chilkat.PublicKey;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sbCertBase64: TStringBuilder;
cert: TCert;
pubKey: TPublicKey;
numBits: Integer;
xml: TXml;
binDat: TBinData;
begin
success := False;
// For this example, I have a certificate in raw base64 format (not PEM),
// that looks like this: "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
sbCertBase64 := TStringBuilder.Create;
success := sbCertBase64.LoadFile('qa_data/certs/base64Cert.txt','utf-8');
cert := TCert.Create;
success := cert.LoadFromBase64(sbCertBase64.GetAsString());
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
// Get the public key.
pubKey := TPublicKey.Create;
cert.GetPublicKey(pubKey);
numBits := pubKey.KeySize;
WriteLn('Number of bits = ' + numBits);
// If using an older version of Chilkat, the key size can be obtained like this:
xml := TXml.Create;
xml.LoadXml(pubKey.GetXml());
binDat := TBinData.Create;
binDat.AppendEncoded(xml.GetChildContent('Modulus'),'base64');
numBits := 8 * binDat.NumBytes;
WriteLn('Number of bits = ' + numBits);
sbCertBase64.Free;
cert.Free;
pubKey.Free;
xml.Free;
binDat.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.