PowerBuilder Requires Chilkat v11.6.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
oleobject loo_Crypt
string ls_Password
oleobject loo_Json
string ls_PhcHash
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
if li_rc < 0 then
destroy loo_Crypt
MessageBox("Error","Connecting to COM object failed")
return
end if
// The password should come from a secure source rather than being hard-coded.
ls_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.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.UpdateString("variant","argon2id")
loo_Json.UpdateInt("memoryCostKb",131072)
loo_Json.UpdateInt("iterations",4)
loo_Json.UpdateInt("parallelism",2)
loo_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.
loo_Json.UpdateString("secret","application-wide-pepper")
loo_Json.UpdateString("secretEncoding","utf-8")
ls_PhcHash = loo_Crypt.Argon2HashPassword(ls_Password,loo_Json.Emit())
if loo_Crypt.LastMethodSuccess = 0 then
Write-Debug loo_Crypt.LastErrorText
destroy loo_Crypt
destroy loo_Json
return
end if
Write-Debug ls_PhcHash
destroy loo_Crypt
destroy loo_Json