/*************************************** **************************************
* Name: MD5 Algorithm Implementation of T-SQL
* Author: Rambo Qian
* Create Date: 2003-04-10
* Last modified by: Rambo Qian
* Last update date: 2003-04-16
* Version: v1.0.00
**************************************** *************************************/
Go
If 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: constant Array
**************************************** *************************************/
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
If 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: constant Array
**************************************** *************************************/
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
If 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 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
If 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 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
If 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 int
With Encryption
As
Begin
Return (DBO. md5_lshift (@ ivalue, @ ishiftbits) | DBO. md5_rshift (@ ivalue, (32-@ ishiftbits )))
End
Go
If 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 int
With Encryption
As
Begin
Declare @ IRES bigint
Set @ IRES = cast (@ IX as binary (8) as bigint) + Cast (cast (@ Iy as binary (8) as bigint)
Return (cast (@ IRES & 0x00000000ffffffff as binary (4 )))
End
Go
If 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 int
With Encryption
As
Begin
Return (@ X & @ Y) | ((~ @ X) & @ z ))
End
Go
If 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 int
With Encryption
As
Begin
Return (@ X & @ z) | (@ Y &(~ @ Z )))
End
Go
If 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 int
With Encryption
As
Begin
Return (@ x ^ @ y ^ @ Z)
End
Go
If 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 int
With Encryption
As
Begin
Return (@ y ^ (@ X | (~ @ Z )))
End
Go
If 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 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 (@)
End
Go
If 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 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 (@)
End
Go
If 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 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 (@)
End
Go
If 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 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 (@)
End
Go