程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> MD5算法的TSQL實現(FOR SQL2000)(二)

MD5算法的TSQL實現(FOR SQL2000)(二)

編輯:關於SqlServer
)(二)。 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(SEL
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved