IF EXISTS(SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[MD5_ConvertTowordArray]') AND xtype IN(N'FN', N'IF', N'TF'))
DROP FUNCTION [dbo].[MD5_ConvertTowordArray]
GO
/*****************************************************************************
* Name: MD5_ConvertTowordArray
* Description: MD5_ConvertTowordArray
*****************************************************************************/
CREATE FUNCTION dbo.MD5_ConvertTowordArray(
@sOrigMess VARCHAR(8000) = '
)
RETURNS @twordArray TABLE([ID] INT IDENTITY(0,1),[word] INT)
WITH ENCRYPTION
AS
BEGIN
IF @sOrigMess IS NULL
SET @sOrigMess = '
DECLARE @iLenOfMess INT
DECLARE @iwordArrayLen INT
DECLARE @iPosOfword INT
DECLARE @iPosOfMess INT
DECLARE @iCountOfword INT
SET @iLenOfMess = LEN(@sOrigMess)
SET @iwordArrayLen = ((@iLenOfMess + 8)/64 + 1) * 16
SET @iCountOfword = 0
WHILE(@iCountOfword<@iwordArrayLen)
BEGIN
INSERT INTO @twordArray([word]) VALUES(0)
SET @iCountOfword = @iCountOfword + 1
END
SELECT @iPosOfMess = 0, @iPosOfword = 0, @iCountOfword = 0
WHILE(@iPosOfMess < @iLenOfMess)
BEGIN
SELECT @iCountOfword = @iPosOfMess / 4, @iPosOfword = @iPosOfMess % 4
UPDATE @twordArray
SET [word] = [word] | dbo.MD5_LShift(UNICODE(SUBSTRING(@sOrigMess,@iPosOfMess+1,1)),@iPosOfword*8)
WHERE [ID] = @iCountOfword
SET @iPosOfMess = @iPosOfMess + 1
END
SELECT @iCountOfword = @iPosOfMess / 4, @iPosOfword = @iPosOfMess % 4
UPDATE @twordArray
SET [word] = [word] | dbo.MD5_LShift(0x80,@iPosOfword*8)
WHERE [ID] = @iCountOfword
UPDATE @twordArray
SET [word] = [word] | dbo.MD5_LShift(@iLenOfMess,3)
WHERE [ID] = @iwordArrayLen - 2
UPDATE @twordArray
SET [word] = [word] | dbo.MD5_RShift(@iLenOfMess,29)
WHERE [ID] = @iwordArrayLen - 1
RETURN
END
GO
IF EXISTS(SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[MD5_wordToHex]') AND xtype IN(N'FN', N'IF', N'TF'))
DROP FUNCTION [dbo].[MD5_wordToHex]
GO
/*****************************************************************************
* Name: MD5_wordToHex
* Description: MD5_wordToHex
*****************************************************************************/
CREATE FUNCTION dbo.MD5_wordToHex(
@iValue INT
)
RETURNS CHAR(8)
WITH ENCRYPTION
AS
BEGIN
DECLARE @sRes VARCHAR(8)
DECLARE @iTmp INT
DECLARE @iCount TINYINT
SELECT @sRes = ', @iCount = 0
WHILE(@iCount<4)
BEGIN
SET @iTmp = dbo.MD5_RShift(@iValue,@iCount*8) & 0x000000FF
SET @sRes = @sRes + CASE @iTmp / 16 WHEN 0 THEN '0'
WHEN 1 THEN '1'
WHEN 2 THEN '2'
WHEN 3 THEN '3'
WHEN 4 THEN '4'
WHEN 5 THEN '5'
WHEN 6 THEN '6'
WHEN 7 THEN '7'
WHEN 8 THEN '8'
WHEN 9 THEN '9'
WHEN 10 THEN 'A'
WHEN 11 THEN 'B'
WHEN 12 THEN 'C'
WHEN 13 THEN 'D'
WHEN 14 THEN 'E'
WHEN 15 THEN 'F'
ELSE ' END
+ CASE @iTmp % 16 WHEN 0 THEN '0'
WHEN 1 THEN '1'
WHEN 2 THEN '2'
WHEN 3 THEN '3'
WHEN 4 THEN '4'
WHEN 5 THEN '5'
WHEN 6 THEN '6'