(SQL Server) MD5 Hash a String (such as a password string)
Demonstrates how to MD5 hash a string to get MD5 hash in hex encoded string representation. (The MD5 hash is 16 bytes, and therefore a hex encoded MD5 hash would be 32 chars.)
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @password nvarchar(4000)
SELECT @password = 'myPassword'
DECLARE @crypt int
-- Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0
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', 'md5'
EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex'
DECLARE @md5Hex nvarchar(4000)
EXEC sp_OAMethod @crypt, 'HashStringENC', @md5Hex OUT, @password
PRINT 'MD5 hash (as a hex string) = ' + @md5Hex
-- The hex string will be uppercase. Your application
-- can easily convert it to lowercase if desired via non-Chilkat means.
EXEC @hr = sp_OADestroy @crypt
END
GO
|