Sample code for 30+ languages & platforms
SQL Server

Example: Crypt2.HashStringENC method

Demonstrates how to call the HashStringENC method.

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

    DECLARE @s nvarchar(4000)
    SELECT @s = 'får'

    -- The string "får" in utf-8 is composed of these bytes: 0x66 0xC3 0xA5 0x72
    EXEC sp_OASetProperty @crypt, 'Charset', 'utf-8'

    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex_lower'
    EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'sha256'

    -- Get the hex string for the sha-256 hash of the utf-8 byte representation of the string
    DECLARE @hashStr nvarchar(4000)
    EXEC sp_OAMethod @crypt, 'HashStringENC', @hashStr OUT, @s


    PRINT @hashStr

    -- Output:
    -- cb8f773dd592337ed7f928012a60f48e1efb357beeda124a54461410e216fece

    EXEC @hr = sp_OADestroy @crypt


END
GO