MD5算法的T-SQL实现(FOR SQL2000)(一)

2023-12-17 06:18

本文主要是介绍MD5算法的T-SQL实现(FOR SQL2000)(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/*****************************************************************************
* 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
*****************************************************************************/
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: 常数组
*****************************************************************************/
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: 常数组
*****************************************************************************/
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(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(@a)
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(@a)
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(@a)
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(@a)
END
GO

 

这篇关于MD5算法的T-SQL实现(FOR SQL2000)(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/503380

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要