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

Verify a Password against an Argon2 Hash

Demonstrates Crypt2.Argon2VerifyPassword, which verifies a password against a PHC-format Argon2 hash string. The cost parameters are read from the hash string, so none need to be supplied.

Background. The method returns an integer: 1 if the password matched, 0 if it did not match, and -1 if the verification could not be performed at all (the hash string was invalid, its parameters were out of range, or an internal failure occurred, with the reason in LastErrorText). Always test for a match with == 1, never with != 0.

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;
  verifyResult: Integer;

begin
  crypt := TCrypt2.Create;

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

  //  A PHC-format Argon2 hash string previously produced by Argon2HashPassword.  All cost options
  //  (variant, version, memory cost, iterations, parallelism, salt, hash length) are read from this
  //  string, so none need to be supplied.
  phcHash := '$argon2id$v=19$m=65536,t=3,p=1$c29tZXJhbmRvbXNhbHQ$3fJ7v1qKcVJ0lHqXjBQZ8mYm3sNTfSPRt0bqDl9kEyM';

  //  Verify the password.  Pass an empty options string when no secret (pepper) or ad was used.
  //  The method returns one of three values:  1 = the password matched, 0 = it did not match, and
  //  -1 = the verification could not be performed (the hash string was invalid or unusable).
  //  Always test for a match with == 1.
  verifyResult := crypt.Argon2VerifyPassword(password,'',phcHash);
  if (verifyResult = 1) then
    begin
      WriteLn('The password is verified.');
    end
  else
    begin
      if (verifyResult = 0) then
        begin
          WriteLn('The password does not match.');
        end
      else
        begin
          WriteLn('The verification could not be performed: ' + crypt.LastErrorText);
        end;
    end;


  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.