Sample code for 30+ languages & platforms
SQL Server

Password_Digest = Base64 (SHA-1 ( nonce + created + SHA-1 (password) ) )

See more Encryption Examples

Demonstrates how to compute:
Password_Digest = Base64 (SHA-1 ( nonce + created + SHA-1 (password)))

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @password nvarchar(4000)
    SELECT @password = 'secret'

    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'SHA-1'
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64'

    -- Generate a 16-byte random nonce
    DECLARE @prng int
    EXEC @hr = sp_OACreate 'Chilkat.Prng', @prng OUT

    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    DECLARE @success int
    EXEC sp_OAMethod @prng, 'GenRandomBd', @success OUT, 16, @bd

    -- Get the current date/time in a string with this format: 2010-06-08T07:26:50Z
    DECLARE @dt int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT

    EXEC sp_OAMethod @dt, 'SetFromCurrentSystemTime', @success OUT
    DECLARE @created nvarchar(4000)
    EXEC sp_OAMethod @dt, 'GetAsTimestamp', @created OUT, 0
    EXEC sp_OAMethod @bd, 'AppendString', @success OUT, @created, 'utf-8'

    -- This example wishes to calculate a password digest like this:
    -- Password_Digest = Base64 ( SHA-1 ( nonce + created + SHA-1(password) ) )

    -- First SHA-1 digest the password...
    DECLARE @passwordSha1 nvarchar(4000)
    EXEC sp_OAMethod @crypt, 'HashStringENC', @passwordSha1 OUT, @password
    -- Append the 20 binary bytes of the SHA1 hash to bd, which already contains the nonce and created date/time.
    EXEC sp_OAMethod @bd, 'AppendEncoded', @success OUT, @passwordSha1, 'base64'

    DECLARE @passwordDigest nvarchar(4000)
    EXEC sp_OAMethod @crypt, 'HashBdENC', @passwordDigest OUT, @bd


    PRINT 'Base64 password digest = ' + @passwordDigest

    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @prng
    EXEC @hr = sp_OADestroy @bd
    EXEC @hr = sp_OADestroy @dt


END
GO