分享
 
 
 

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

王朝mssql·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

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

BEGIN

INSERT INTO @tWordArray([Word]) VALUES(0)

SET @iCountOfWord = @iCountOfWord + 1

END

SELECT @iPosOfMess = 0, @iPosOfWord = 0, @iCountOfWord = 0

WHILE(@iPosOfMess

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

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'

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

SET @iCount = @iCount + 1

END

RETURN(@sRes)

END

GO

IF EXISTS(SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[MD5]') AND xtype IN(N'FN', N'IF', N'TF'))

DROP FUNCTION [dbo].[MD5]

GO

/*****************************************************************************

* Name: MD5

* Description: MD5

*****************************************************************************/

CREATE FUNCTION dbo.MD5(

@sOrigMess

NVARCHAR(4000)

)

RETURNS CHAR(32)

WITH ENCRYPTION

AS

BEGIN

--====================================

DECLARE @S11 TINYINT

DECLARE @S12 TINYINT

DECLARE @S13 TINYINT

DECLARE @S14 TINYINT

DECLARE @S21 TINYINT

DECLARE @S22 TINYINT

DECLARE @S23 TINYINT

DECLARE @S24 TINYINT

DECLARE @S31 TINYINT

DECLARE @S32 TINYINT

DECLARE @S33 TINYINT

DECLARE @S34 TINYINT

DECLARE @S41 TINYINT

DECLARE @S42 TINYINT

DECLARE @S43 TINYINT

DECLARE @S44 TINYINT

SELECT @S11 = 7, @S12 = 12, @S13 = 17, @S14 = 22

SELECT @S21 = 5, @S22 = 9, @S23 = 14, @S24 = 20

SELECT @S31 = 4, @S32 = 11, @S33 = 16, @S34 = 23

SELECT @S41 = 6, @S42 = 10, @S43 = 15, @S44 = 21

--====================================

DECLARE @a INT

DECLARE @b INT

DECLARE @c INT

DECLARE @d INT

DECLARE @AA

INT

DECLARE @BB

INT

DECLARE @CC

INT

DECLARE @DD

INT

SELECT

@a = 0x67452301

,@b = 0xEFCDAB89

,@c = 0x98BADCFE

,@d = 0x10325476

--====================================

DECLARE @sRes

VARCHAR(32)

SET @sRes = ''

DECLARE @iWordArrayLen

INT

DECLARE @iWordArrayCount

INT

DECLARE @tTmp TABLE([ID] INT, [Word] INT)

INSERT INTO @tTmp SELECT * FROM dbo.MD5_ConvertToWordArray(@sOrigMess)

SELECT @iWordArrayCount=0, @iWordArrayLen = COUNT(*)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有