MD5 encryption function written in T-SQL (to 1)

Source: Internet
Author: User

MD5 Encryption

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
**************************************** **************************************** **

Create Function DBO. md5_encode (
@ Para tinyint
)
Returns int
With Encryption
As
Begin
Declare @ BYT int
Select @ BYT =
Case @ para
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 (@ BYT)
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
--*************************************** **************************************
Create Function DBO. md5_m_2power (
@ Para tinyint
)
Returns int
With Encryption
As
Begin
Declare @ BYT int
Select @ BYT =
Case @ para
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 (@ BYT)
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
---*************************************** **************************************
Create Function DBO. md5_lshift (
@ Ivalue int
, @ Ishiftbits tinyint
)
Returns int
With Encryption
As
Begin
Declare @ BYT bigint
Set @ BYT = cast (@ ivalue as binary (8 ))
Set @ BYT = @ BYT * DBO. md5_m_2power (@ ishiftbits)
Return (cast (@ BYT & 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
/*************************************** **************************************/
Create Function DBO. md5_rshift (
@ Ivalue int
, @ Ishiftbits tinyint
)
Returns int
With Encryption
As
Begin
Declare @ BYT bigint
Set @ BYT = cast (@ ivalue as binary (8 ))
Set @ BYT = @ BYT/DBO. md5_m_2power (@ ishiftbits)
Return (cast (@ BYT & 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
/*************************************** **************************************/

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
/*************************************** **************************************/
Create Function DBO. md5_addunsigned (
@ IX int
, @ Iy int
)
Returns int
With Encryption
As
Begin
Declare @ BYT bigint
Set @ BYT = cast (@ IX as binary (8) as bigint) + Cast (cast (@ Iy as binary (8) as bigint)
Return (cast (@ BYT & 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
/*************************************** *************************************/
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
/*************************************** **************************************/

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
/*************************************** **************************************/

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
/*************************************** **************************************/
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
/*************************************** **************************************/

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
/*************************************** **************************************/
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.