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

Get a Private Key's Raw Hex Value

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.GetRawHex method, which returns the raw private value as lowercase hexadecimal and replaces the contents of the supplied StringBuilder with the corresponding raw public key.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This drops below the structured container formats to the bare key bytes as hex, which is meaningful chiefly for fixed-size keys such as EC and Ed25519 where a key is essentially a fixed byte string. It is the export to use when another system wants raw values rather than PEM or JWK — the counterpart to LoadEd25519. Both the returned private hex and the public value are produced in one call.

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.PrivateKey,
  Chilkat.StringBuilder;

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

procedure RunDemo;
var
  success: Boolean;
  privKey: TPrivateKey;
  sbPublic: TStringBuilder;
  privHex: string;

begin
  success := False;

  //  Load a private key to export.
  privKey := TPrivateKey.Create;
  success := privKey.LoadPemFile('qa_data/private.pem');
  if (success = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  //  GetRawHex returns the raw private value as lowercase hex, and replaces the contents of the
  //  StringBuilder argument with the corresponding raw public key.  This is most meaningful for
  //  fixed-size keys such as EC and Ed25519.
  sbPublic := TStringBuilder.Create;
  privHex := privKey.GetRawHex(sbPublic);
  if (privKey.LastMethodSuccess = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  WriteLn('Raw private value: ' + privHex);
  WriteLn('Raw public key: ' + sbPublic.GetAsString());


  privKey.Free;
  sbPublic.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.