Thursday, September 8, 2011

MD5 Sqr server password encrypt

CREATE FUNCTION [dbo].[fn_MD5]
(
    @string AS VARCHAR(MAX),
    @chars AS INT
)
RETURNS NVARCHAR(32)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @md5 NVARCHAR(32)

    SELECT @md5 = SUBSTRING(CAST(master.dbo.fn_varbintohexstr(HashBytes('MD5',@string )) AS NVARCHAR(32)),3,@chars )

    -- Return the result of the function
    RETURN @md5
END

select dbo.fn_MD5('hello', 32) as md5
select dbo.fn_MD5('rakeshwalia', 40) as md5
select dbo.fn_MD5('sachinrameshtendulkarserttuiklolioooooo', 40) as md5

No comments:

Post a Comment