Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.6.0+

Hash a Password with Argon2 (Defaults)

Demonstrates Crypt2.Argon2HashPassword, which hashes a password with Argon2 and returns a PHC-format string. Passing an empty options string uses all defaults, including a random salt.

Background. Argon2 (RFC 9106) is a memory-hard function that is deliberately expensive in CPU time and memory, which is what makes brute-forcing the password space costly. The argon2id variant is recommended unless there is a specific reason to choose argon2i or argon2d. The returned PHC string records the variant, version, cost parameters, salt, and hash — everything needed to verify the password later. Store the whole string.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.Crypt2;

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

procedure RunDemo;
var
  crypt: TCrypt2;
  password: string;
  phcHash: string;

begin
  crypt := TCrypt2.Create;

  //  The password should come from a secure source rather than being hard-coded.
  password := 'correct horse battery staple';

  //  Hash the password using all defaults by passing an empty options string.  The defaults are
  //  Argon2id, version 19, 3 iterations, 64 MB of memory, 1 lane, a 32-byte hash, and a 16-byte random
  //  salt.  A random salt is generated automatically, which is what should normally happen.
  phcHash := crypt.Argon2HashPassword(password,'');
  if (crypt.LastMethodSuccess = False) then
    begin
      WriteLn(crypt.LastErrorText);
      Exit;
    end;

  //  The returned PHC-format string records the variant, version, cost parameters, salt, and hash --
  //  everything needed to verify the password later.  Store the whole string.
  WriteLn(phcHash);


  crypt.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.