Lazarus Pascal Requires Chilkat v11.6.0+
Lazarus Pascal
Hash a Password with Argon2 (Tuned Options)
Demonstrates Crypt2.Argon2HashPassword with explicit options: the cost parameters, saltLen (how many random salt bytes to generate when no salt is given), and an optional secret (pepper).
Background. For hashing, the salt is optional — when omitted, a random salt of
saltLen bytes (default 16) is generated, which is the normal case. A secret or ad is not stored in the PHC string; the same values must be supplied to Argon2VerifyPassword for the password to verify.Chilkat Lazarus Pascal 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.Crypt2,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
crypt: TCrypt2;
password: string;
json: TJsonObject;
phcHash: string;
begin
crypt := TCrypt2.Create;
// The password should come from a secure source rather than being hard-coded.
password := 'correct horse battery staple';
// The Argon2 options are the same members as Argon2DeriveKey, except the salt is optional for
// hashing. When no "salt" is given, a random salt is generated; "saltLen" (default 16, range 8..1024)
// sets how many random bytes to use.
json := TJsonObject.Create;
json.UpdateString('variant','argon2id');
json.UpdateInt('memoryCostKb',131072);
json.UpdateInt('iterations',4);
json.UpdateInt('parallelism',2);
json.UpdateInt('saltLen',16);
// A secret (pepper) and/or ad may be used, but neither is stored in the returned PHC string. The
// same values must be supplied to Argon2VerifyPassword for the password to verify.
json.UpdateString('secret','application-wide-pepper');
json.UpdateString('secretEncoding','utf-8');
phcHash := crypt.Argon2HashPassword(password,json.Emit());
if (crypt.LastMethodSuccess = False) then
begin
WriteLn(crypt.LastErrorText);
Exit;
end;
WriteLn(phcHash);
crypt.Free;
json.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.