MD5算法的T-SQL实现(FOR SQL2000)(二)

寻找舒晨

寻找舒晨

2016-01-29 16:40

MD5算法的T-SQL实现(FOR SQL2000)(二),MD5算法的T-SQL实现(FOR SQL2000)(二)
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'
展开更多 50%)
分享

猜你喜欢

MD5算法的T-SQL实现(FOR SQL2000)(二)

SQLServer
MD5算法的T-SQL实现(FOR SQL2000)(二)

MD5算法的T-SQL实现(FOR SQL2000)(一)

SQLServer
MD5算法的T-SQL实现(FOR SQL2000)(一)

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

SQL Server 管理常用的SQL和T-SQL(二)

SQLServer
SQL Server 管理常用的SQL和T-SQL(二)

MD5算法在JS中的实现

电脑网络
MD5算法在JS中的实现

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

MD5算法研究(2)

ASP
MD5算法研究(2)

SQL Server 管理常用的SQL和T-SQL

SQLServer
SQL Server 管理常用的SQL和T-SQL

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

怎样配置SQL Server发送电子邮件

怎样配置SQL Server发送电子邮件

深入浅出谈防火墙

深入浅出谈防火墙
下拉加载更多内容 ↓