/****************************************************************************** name: t-sql md5算法实现* author: rambo qian* create date: 2003-04-10* last modified by: rambo qian* last update date: 2003-04-16* version: v1.0.00*****************************************************************************/goif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_m_onbits]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_m_onbits]go/****************************************************************************** name: md5_m_onbits* description: 常数组*****************************************************************************/create function dbo.md5_m_onbits( @i tinyint)returns intwith encryptionasbegin 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)endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_m_2power]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_m_2power]go/****************************************************************************** name: md5_m_2power* description: 常数组*****************************************************************************/create function dbo.md5_m_2power( @i tinyint)returns intwith encryptionasbegin 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)endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_lshift]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_lshift]go/****************************************************************************** name: md5_lshift* description: md5_lshift*****************************************************************************/create function dbo.md5_lshift( @ivalue int ,@ishiftbits tinyint)returns intwith encryptionasbegin 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)))endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_rshift]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_rshift]go/****************************************************************************** name: md5_rshift* description: md5_rshift*****************************************************************************/create function dbo.md5_rshift( @ivalue int ,@ishiftbits tinyint)returns intwith encryptionasbegin 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)))endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_rotateleft]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_rotateleft]go/****************************************************************************** name: md5_rotateleft* description: md5_rotateleft*****************************************************************************/create function dbo.md5_rotateleft( @ivalue int ,@ishiftbits tinyint)returns intwith encryptionasbegin return(dbo.md5_lshift(@ivalue, @ishiftbits) dbo.md5_rshift(@ivalue, (32 - @ishiftbits)))endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_addunsigned]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_addunsigned]go/****************************************************************************** name: md5_addunsigned* description: md5_addunsigned*****************************************************************************/create function dbo.md5_addunsigned( @ix int ,@iy int)returns intwith encryptionasbegin 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)))endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_f]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_f]go/****************************************************************************** name: md5_f* description: md5_f*****************************************************************************/create function dbo.md5_f( @x int ,@y int ,@z int)returns intwith encryptionasbegin return((@x & @y) ((~@x) & @z))endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_g]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_g]go/****************************************************************************** name: md5_g* description: md5_g*****************************************************************************/create function dbo.md5_g( @x int ,@y int ,@z int)returns intwith encryptionasbegin return((@x & @z) (@y & (~@z)))endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_h]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_h]go/****************************************************************************** name: md5_h* description: md5_h*****************************************************************************/create function dbo.md5_h( @x int ,@y int ,@z int)returns intwith encryptionasbegin return(@x ^ @y ^ @z)endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_i]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_i]go/****************************************************************************** name: md5_i* description: md5_i*****************************************************************************/create function dbo.md5_i( @x int ,@y int ,@z int)returns intwith encryptionasbegin return(@y ^ (@x (~@z)))endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_ff]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_ff]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 intwith encryptionasbegin 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)endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_gg]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_gg]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 intwith encryptionasbegin 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)endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_hh]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_hh]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 intwith encryptionasbegin 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)endgoif exists(select * from dbo.sysobjects where id = object_id(n'[dbo].[md5_ii]') and xtype in(n'fn', n'if', n'tf')) drop function [dbo].[md5_ii]go/****************************************************************************** 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 intwith encryptionasbegin 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)endgo
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器