--sql server中的md5实现方法
/*****************************************************************************
* name: md5_ii
* description: md5_ii
*****************************************************************************/
create function dbo.md5_ii(
@a int,
@b int,
@c int,
@d int,
@x int,
@s int,
@ac int
)
returns int
with encryption
as
begin
set @a = dbo.md5_addunsigned(@a, dbo.md5_addunsigned(dbo.md5_addunsigned(dbo.md5_i(@b, @c, @d), @x), @ac))
set @a = dbo.md5_rotateleft(@a, @s)
set @a = dbo.md5_addunsigned(@a, @b)
return(@a)
end
go
/*****************************************************************************
* name: md5_hh
* description: md5_hh
*****************************************************************************/
create function dbo.md5_hh(
@a int,
@b int,
@c int,
@d int,
@x int,
@s int,
@ac int
)
returns int
with encryption
as
begin
set @a = dbo.md5_addunsigned(@a, dbo.md5_addunsigned(dbo.md5_addunsigned(dbo.md5_h(@b, @c, @d), @x), @ac))
set @a = dbo.md5_rotateleft(@a, @s)
set @a = dbo.md5_addunsigned(@a, @b)
return(@a)
end
go
/*****************************************************************************
* name: md5_gg
* description: md5_gg
*****************************************************************************/
create function dbo.md5_gg(
@a int,
@b int,
@c int,
@d int,
@x int,
@s int,
@ac int
)
returns int
with encryption
as
begin
set @a = dbo.md5_addunsigned(@a, dbo.md5_addunsigned(dbo.md5_addunsigned(dbo.md5_g(@b, @c, @d), @x), @ac))
set @a = dbo.md5_rotateleft(@a, @s)
set @a = dbo.md5_addunsigned(@a, @b)
return(@a)
end
go
/*****************************************************************************
* name: md5_ff
* description: md5_ff
*****************************************************************************/
create function dbo.md5_ff(
@a int,
@b int,
@c int,
@d int,
@x int,
@s int,
@ac int
)
returns int
with encryption
as
begin
set @a = dbo.md5_addunsigned(@a, dbo.md5_addunsigned(dbo.md5_addunsigned(dbo.md5_f(@b, @c, @d), @x), @ac))
set @a = dbo.md5_rotateleft(@a, @s)
set @a = dbo.md5_addunsigned(@a, @b)
return(@a)
end
go
/*****************************************************************************
* name: md5_i
* description: md5_i
*****************************************************************************/
create function dbo.md5_i(
@x int
,@y int
,@z int
)
returns int
with encryption
as
begin
return(@y ^ (@x | (~@z)))
end
go
/*****************************************************************************
* name: md5_h
* description: md5_h
*****************************************************************************/
create function dbo.md5_h(
@x int,
@y int,
@z int
)
returns int
with encryption
as
begin
return(@x ^ @y ^ @z)
end
go
/*****************************************************************************
* name: md5_g
* description: md5_g
*****************************************************************************/
create function dbo.md5_g(
@x int,
@y int,
@z int
)
returns int
with encryption
as
begin
return((@x & @z) | (@y & (~@z)))
end
go
/*****************************************************************************
* name: md5_f
* description: md5_f
*****************************************************************************/
create function dbo.md5_f(
@x int,
@y int,
@z int
)
returns int
with encryption
as
begin
return((@x & @y) | ((~@x) & @z))
end
go
/*****************************************************************************
* name: md5_addunsigned
* description: md5_addunsigned
*****************************************************************************/
create function dbo.md5_addunsigned(
@ix int,
@iy int
)
returns int
with encryption
as
begin
declare @ires bigint
set @ires = cast(cast(@ix as binary(8)) as bigint) + cast(cast(@iy as binary(8)) as bigint)
return(cast(@ires & 0x00000000ffffffff as binary(4)))
end
go
/*****************************************************************************
* name: md5_rotateleft
* description: md5_rotateleft
*****************************************************************************/
create function dbo.md5_rotateleft(
@ivalue int,
@ishiftbits tinyint
)
returns int
with encryption
as
begin
return(dbo.md5_lshift(@ivalue, @ishiftbits) | dbo.md5_rshift(@ivalue, (32 - @ishiftbits)))
end
go
/*****************************************************************************
* name: md5_rshift
* description: md5_rshift
*****************************************************************************/
create function dbo.md5_rshift(
@ivalue int ,
@ishiftbits tinyint
)
returns int
with encryption
as
begin
declare @ires bigint
set @ires = cast(@ivalue as binary(8))
set @ires = @ires / dbo.md5_m_2power(@ishiftbits)
return(cast(@ires & 0x00000000ffffffff as binary(4)))
end
go
/*****************************************************************************
* name: md5_lshift
* description: md5_lshift
*****************************************************************************/
create function dbo.md5_lshift(
@ivalue int,
@ishiftbits tinyint
)
returns int
with encryption
as
begin
declare @ires bigint
set @ires = cast(@ivalue as binary(8))
set @ires = @ires * dbo.md5_m_2power(@ishiftbits)
return(cast(@ires & 0x00000000ffffffff as binary(4)))
end
go
/*****************************************************************************
* name: md5_m_2power
* description: 常数组
*****************************************************************************/
create function dbo.md5_m_2power(
@i tinyint
)
returns int
with encryption
as
begin
declare @ires int
select @ires =
case @i
when 0 then 1 -- 00000000000000000000000000000001
when 1 then 2 -- 00000000000000000000000000000010
when 2 then 4 -- 00000000000000000000000000000100
when 3 then 8 -- 00000000000000000000000000001000
when 4 then 16 -- 00000000000000000000000000010000
when 5 then 32 -- 00000000000000000000000000100000
when 6 then 64 -- 00000000000000000000000001000000
when 7 then 128 -- 00000000000000000000000010000000
when 8 then 256 -- 00000000000000000000000100000000
when 9 then 512 -- 00000000000000000000001000000000
when 10 then 1024 -- 00000000000000000000010000000000
when 11 then 2048 -- 00000000000000000000100000000000
when 12 then 4096 -- 00000000000000000001000000000000
when 13 then 8192 -- 00000000000000000010000000000000
when 14 then 16384 -- 00000000000000000100000000000000
when 15 then 32768 -- 00000000000000001000000000000000
when 16 then 65536 -- 00000000000000010000000000000000
when 17 then 131072 -- 00000000000000100000000000000000
when 18 then 262144 -- 00000000000001000000000000000000
when 19 then 524288 -- 00000000000010000000000000000000
when 20 then 1048576 -- 00000000000100000000000000000000
when 21 then 2097152 -- 00000000001000000000000000000000
when 22 then 4194304 -- 00000000010000000000000000000000
when 23 then 8388608 -- 00000000100000000000000000000000
when 24 then 16777216 -- 00000001000000000000000000000000
when 25 then 33554432 -- 00000010000000000000000000000000
when 26 then 67108864 -- 00000100000000000000000000000000
when 27 then 134217728 -- 00001000000000000000000000000000
when 28 then 268435456 -- 00010000000000000000000000000000
when 29 then 536870912 -- 00100000000000000000000000000000
when 30 then 1073741824 -- 01000000000000000000000000000000
else 0
end
return(@ires)
end
go
/*****************************************************************************
* name: md5_m_onbits
* description: 常数组
*****************************************************************************/
create function dbo.md5_m_onbits(
@i tinyint
)
returns int
with encryption
as
begin
declare @ires int
select @ires =
case @i
when 0 then 1 -- 00000000000000000000000000000001
when 1 then 3 -- 00000000000000000000000000000011
when 2 then 7 -- 00000000000000000000000000000111
when 3 then 15 -- 00000000000000000000000000001111
when 4 then 31 -- 00000000000000000000000000011111
when 5 then 63 -- 00000000000000000000000000111111
when 6 then 127 -- 00000000000000000000000001111111
when 7 then 255 -- 00000000000000000000000011111111
when 8 then 511 -- 00000000000000000000000111111111
when 9 then 1023 -- 00000000000000000000001111111111
when 10 then 2047 -- 00000000000000000000011111111111
when 11 then 4095 -- 00000000000000000000111111111111
when 12 then 8191 -- 00000000000000000001111111111111
when 13 then 16383 -- 00000000000000000011111111111111
when 14 then 32767 -- 00000000000000000111111111111111
when 15 then 65535 -- 00000000000000001111111111111111
when 16 then 131071 -- 00000000000000011111111111111111
when 17 then 262143 -- 00000000000000111111111111111111
when 18 then 524287 -- 00000000000001111111111111111111
when 19 then 1048575 -- 00000000000011111111111111111111
when 20 then 2097151 -- 00000000000111111111111111111111
when 21 then 4194303 -- 00000000001111111111111111111111
when 22 then 8388607 -- 00000000011111111111111111111111
when 23 then 16777215 -- 00000000111111111111111111111111
when 24 then 33554431 -- 00000001111111111111111111111111
when 25 then 67108863 -- 00000011111111111111111111111111
when 26 then 134217727 -- 00000111111111111111111111111111
when 27 then 268435455 -- 00001111111111111111111111111111
when 28 then 536870911 -- 00011111111111111111111111111111
when 29 then 1073741823 -- 00111111111111111111111111111111
when 30 then 2147483647 -- 01111111111111111111111111111111
else 0
end
return(@ires)
end
go
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
/*****************************************************************************
* 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
/*****************************************************************************
* 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(*) from @ttmp
while(@iwordarraycount begin
select @aa = @a, @bb = @b, @cc = @c, @dd = @d
select @a = dbo.md5_ff(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 0), @s11, 0xd76aa478)
select @d = dbo.md5_ff(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 1), @s12, 0xe8c7b756)
select @c = dbo.md5_ff(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 2), @s13, 0x242070db)
select @b = dbo.md5_ff(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 3), @s14, 0xc1bdceee)
select @a = dbo.md5_ff(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 4), @s11, 0xf57c0faf)
select @d = dbo.md5_ff(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 5), @s12, 0x4787c62a)
select @c = dbo.md5_ff(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 6), @s13, 0xa8304613)
select @b = dbo.md5_ff(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 7), @s14, 0xfd469501)
select @a = dbo.md5_ff(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 8), @s11, 0x698098d8)
select @d = dbo.md5_ff(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 9), @s12, 0x8b44f7af)
select @c = dbo.md5_ff(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 10), @s13, 0xffff5bb1)
select @b = dbo.md5_ff(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 11), @s14, 0x895cd7be)
select @a = dbo.md5_ff(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 12), @s11, 0x6b901122)
select @d = dbo.md5_ff(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 13), @s12, 0xfd987193)
select @c = dbo.md5_ff(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 14), @s13, 0xa679438e)
select @b = dbo.md5_ff(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 15), @s14, 0x49b40821)
select @a = dbo.md5_gg(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 1), @s21, 0xf61e2562)
select @d = dbo.md5_gg(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 6), @s22, 0xc040b340)
select @c = dbo.md5_gg(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 11), @s23, 0x265e5a51)
select @b = dbo.md5_gg(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 0), @s24, 0xe9b6c7aa)
select @a = dbo.md5_gg(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 5), @s21, 0xd62f105d)
select @d = dbo.md5_gg(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 10), @s22, 0x2441453)
select @c = dbo.md5_gg(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 15), @s23, 0xd8a1e681)
select @b = dbo.md5_gg(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 4), @s24, 0xe7d3fbc8)
select @a = dbo.md5_gg(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 9), @s21, 0x21e1cde6)
select @d = dbo.md5_gg(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 14), @s22, 0xc33707d6)
select @c = dbo.md5_gg(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 3), @s23, 0xf4d50d87)
select @b = dbo.md5_gg(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 8), @s24, 0x455a14ed)
select @a = dbo.md5_gg(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 13), @s21, 0xa9e3e905)
select @d = dbo.md5_gg(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 2), @s22, 0xfcefa3f8)
select @c = dbo.md5_gg(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 7), @s23, 0x676f02d9)
select @b = dbo.md5_gg(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 12), @s24, 0x8d2a4c8a)
select @a = dbo.md5_hh(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 5), @s31, 0xfffa3942)
select @d = dbo.md5_hh(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 8), @s32, 0x8771f681)
select @c = dbo.md5_hh(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 11), @s33, 0x6d9d6122)
select @b = dbo.md5_hh(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 14), @s34, 0xfde5380c)
select @a = dbo.md5_hh(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 1), @s31, 0xa4beea44)
select @d = dbo.md5_hh(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 4), @s32, 0x4bdecfa9)
select @c = dbo.md5_hh(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 7), @s33, 0xf6bb4b60)
select @b = dbo.md5_hh(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 10), @s34, 0xbebfbc70)
select @a = dbo.md5_hh(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 13), @s31, 0x289b7ec6)
select @d = dbo.md5_hh(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 0), @s32, 0xeaa127fa)
select @c = dbo.md5_hh(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 3), @s33, 0xd4ef3085)
select @b = dbo.md5_hh(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 6), @s34, 0x4881d05)
select @a = dbo.md5_hh(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 9), @s31, 0xd9d4d039)
select @d = dbo.md5_hh(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 12), @s32, 0xe6db99e5)
select @c = dbo.md5_hh(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 15), @s33, 0x1fa27cf8)
select @b = dbo.md5_hh(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 2), @s34, 0xc4ac5665)
select @a = dbo.md5_ii(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 0), @s41, 0xf4292244)
select @d = dbo.md5_ii(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 7), @s42, 0x432aff97)
select @c = dbo.md5_ii(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 14), @s43, 0xab9423a7)
select @b = dbo.md5_ii(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 5), @s44, 0xfc93a039)
select @a = dbo.md5_ii(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 12), @s41, 0x655b59c3)
select @d = dbo.md5_ii(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 3), @s42, 0x8f0ccc92)
select @c = dbo.md5_ii(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 10), @s43, 0xffeff47d)
select @b = dbo.md5_ii(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 1), @s44, 0x85845dd1)
select @a = dbo.md5_ii(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 8), @s41, 0x6fa87e4f)
select @d = dbo.md5_ii(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 15), @s42, 0xfe2ce6e0)
select @c = dbo.md5_ii(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 6), @s43, 0xa3014314)
select @b = dbo.md5_ii(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 13), @s44, 0x4e0811a1)
select @a = dbo.md5_ii(@a, @b, @c, @d, (select [word] from @ttmp where [id] = @iwordarraycount + 4), @s41, 0xf7537e82)
select @d = dbo.md5_ii(@d, @a, @b, @c, (select [word] from @ttmp where [id] = @iwordarraycount + 11), @s42, 0xbd3af235)
select @c = dbo.md5_ii(@c, @d, @a, @b, (select [word] from @ttmp where [id] = @iwordarraycount + 2), @s43, 0x2ad7d2bb)
select @b = dbo.md5_ii(@b, @c, @d, @a, (select [word] from @ttmp where [id] = @iwordarraycount + 9), @s44, 0xeb86d391)
set @a = dbo.md5_addunsigned(@a, @aa)
set @b = dbo.md5_addunsigned(@b, @bb)
set @c = dbo.md5_addunsigned(@c, @cc)
set @d = dbo.md5_addunsigned(@d, @dd)
set @iwordarraycount = @iwordarraycount + 16
end
set @sres = dbo.md5_wordtohex(@a) + dbo.md5_wordtohex(@b) + dbo.md5_wordtohex(@c) + dbo.md5_wordtohex(@d)
set @sres = lower(@sres)
return(@sres)
end
go
