SQL Server Requires Chilkat v11.6.0+
SQL Server
Hash a Password with Argon2 (Defaults)
Demonstrates Crypt2.Argon2HashPassword, which hashes a password with Argon2 and returns a PHC-format string. Passing an empty options string uses all defaults, including a random salt.
Background. Argon2 (RFC 9106) is a memory-hard function that is deliberately expensive in CPU time and memory, which is what makes brute-forcing the password space costly. The
argon2id variant is recommended unless there is a specific reason to choose argon2i or argon2d. The returned PHC string records the variant, version, cost parameters, salt, and hash — everything needed to verify the password later. Store the whole string.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'
-- Hash the password using all defaults by passing an empty options string. The defaults are
-- Argon2id, version 19, 3 iterations, 64 MB of memory, 1 lane, a 32-byte hash, and a 16-byte random
-- salt. A random salt is generated automatically, which is what should normally happen.
DECLARE @phcHash nvarchar(4000)
EXEC sp_OAMethod @crypt, 'Argon2HashPassword', @phcHash OUT, @password, ''
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
RETURN
END
-- The returned PHC-format string records the variant, version, cost parameters, salt, and hash --
-- everything needed to verify the password later. Store the whole string.
PRINT @phcHash
EXEC @hr = sp_OADestroy @crypt
END
GO