SQL Server Requires Chilkat v11.6.0+
SQL Server
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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @crypt int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- The password should come from a secure source rather than being hard-coded.
DECLARE @password nvarchar(4000)
SELECT @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.
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
DECLARE @success int
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'variant', 'argon2id'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'memoryCostKb', 131072
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'iterations', 4
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'parallelism', 2
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, '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.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'secret', 'application-wide-pepper'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'secretEncoding', 'utf-8'
DECLARE @phcHash nvarchar(4000)
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @crypt, 'Argon2HashPassword', @phcHash OUT, @password, @sTmp0
EXEC sp_OAGetProperty @crypt, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @json
RETURN
END
PRINT @phcHash
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @json
END
GO