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

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoCrypt
    String sPassword
    Handle hoJson
    Boolean iSuccess
    String sPhcHash
    String sTemp1
    Boolean bTemp1

    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End

    //  The password should come from a secure source rather than being hard-coded.
    Move "correct horse battery staple" To sPassword

    //  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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "variant" "argon2id" To iSuccess
    Get ComUpdateInt Of hoJson "memoryCostKb" 131072 To iSuccess
    Get ComUpdateInt Of hoJson "iterations" 4 To iSuccess
    Get ComUpdateInt Of hoJson "parallelism" 2 To iSuccess
    Get ComUpdateInt Of hoJson "saltLen" 16 To iSuccess

    //  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.
    Get ComUpdateString Of hoJson "secret" "application-wide-pepper" To iSuccess
    Get ComUpdateString Of hoJson "secretEncoding" "utf-8" To iSuccess

    Get ComEmit Of hoJson To sTemp1
    Get ComArgon2HashPassword Of hoCrypt sPassword sTemp1 To sPhcHash
    Get ComLastMethodSuccess Of hoCrypt To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sPhcHash


End_Procedure