SQL SERVER 2000的 SHA1加密算法脚本

    技术2022-05-11  76

    if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA1] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA1 ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA_ADD] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA_ADD ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA_AlignSHA1] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA_AlignSHA1 ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA_Ft] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA_Ft ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA_Hex] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA_Hex ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA_Kt] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA_Kt ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA_LShift] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA_LShift ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA_Power] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA_Power ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA_R2Shift] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA_R2Shift ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA_RShift] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA_RShift ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[SHA_Rol] ' and  xtype  in  (N ' FN ' , N ' IF ' , N ' TF ' )) drop   function   [ dbo ] . [ SHA_Rol ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[RndPWD] ' and   OBJECTPROPERTY (id, N ' IsProcedure ' =   1 ) drop   procedure   [ dbo ] . [ RndPWD ] GO if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[ttt] ' and   OBJECTPROPERTY (id, N ' IsProcedure ' =   1 ) drop   procedure   [ dbo ] . [ ttt ] GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO /*SHA1加密主程序返回40位加密字符串海风编写2005-12-22*/ CREATE   FUNCTION  dbo.SHA1(      @sOrigMess   nvarchar ( 4000 )) RETURNS   CHAR ( 40 ) -- WITH ENCRYPTION AS BEGIN   DECLARE   @a   bigint DECLARE   @b   bigint DECLARE   @c   bigint DECLARE   @d   bigint DECLARE   @e   bigint set   @a = 1732584193 set   @b =- 271733879 set   @c =- 1732584194 set   @d = 271733878 set   @e =- 1009589776 DECLARE   @olda   bigint DECLARE   @oldb   bigint DECLARE   @oldc   bigint DECLARE   @oldd   bigint DECLARE   @olde   bigint declare   @zcnt   int , @i   int , @j   int set   @i = 0 DECLARE   @x   TABLE ( [ ID ]   int [ Word ]   bigint ) DECLARE   @w   TABLE ( [ ID ]   int [ Word ]   bigint ) while ( @i < 80 ) begin INSERT   into   @w  (id,word)  values   ( @i , 0 ) set   @i = @i + 1 end declare   @m1   bigint , @m2   bigint , @m3   bigint , @m4   bigint , @t   bigint , @w_j   bigint INSERT   into   @x   SELECT   *   FROM  dbo.SHA_AlignSHA1( @sOrigMess ) set   @zcnt = @@rowcount set   @i = 0 while ( @i < @zcnt ) begin   set   @olda = @a   set   @oldb = @b   set   @oldc = @c   set   @oldd = @d   set   @olde = @e   set   @j = 0   while ( @j < 80 begin    if   @j < 16     --  w[j]=x[i+j];    begin     set   @w_j = select   word   from   @x   where  id = @i + @j )    update   @w   set  Word =   @w_j     where  id = @i    end    else    begin    -- w[j]=rol(w[j-3]^w[j-8]^w[j-14]^w[j-16],1);     select   @m1   = Word  from   @w   where  id = ( @j - 3 )    select   @m2   = Word  from   @w   where  id = ( @j - 8 )    select   @m3   = Word  from   @w   where  id = ( @j - 14 )    select   @m4   = Word  from   @w   where  id = ( @j - 16 )    set   @w_j = dbo.SHA_Rol( @m1 ^ @m2 ^ @m3 ^ @m4 , 1 )    update    @w    set  Word = @w_j    where  id = @j    end    set   @t = dbo.SHA_ADD(dbo.SHA_ADD(dbo.SHA_Rol( @a , 5 ),dbo.SHA_Ft( @j , @b , @c , @d )),dbo.SHA_ADD(dbo.SHA_ADD( @e , @w_j ),dbo.SHA_kt( @j )))   set   @e = @d    set   @d = @c    set   @c = dbo.SHA_Rol( @b , 30 )   set   @b = @a    set   @a = @t   set   @j = @j + 1   end      set   @a = dbo.SHA_ADD( @a , @olda )     set   @b = dbo.SHA_ADD( @b , @oldb )     set   @c = dbo.SHA_ADD( @c , @oldc )     set   @d = dbo.SHA_ADD( @d , @oldd )     set   @e = dbo.SHA_ADD( @e , @olde ) set   @i = @i + 16 end return  dbo.SHA_Hex( @a ) + dbo.SHA_Hex( @b ) + dbo.SHA_Hex( @c ) + dbo.SHA_Hex( @d ) + dbo.SHA_Hex( @e ) END   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO /*****************************************************************************function add(x,y){  var lsw=(x&0xFFFF)+(y&0xFFFF);  var msw=(x>>16)+(y>>16)+(lsw>>16);  return(msw<<16)|(lsw&0xFFFF);}SHA_RShiftSHA_LShift*****************************************************************************/ CREATE   FUNCTION  dbo.SHA_ADD(      @x          bigint         , @y     bigint  ) RETURNS   bigint AS BEGIN DECLARE   @lsw   bigint , @msw   bigint set     @lsw = ( ( @x & 0xFFFF ) + ( @y & 0xFFFF )) set    @msw =  dbo.SHA_RShift( @x , 16 ) + dbo.SHA_RShift( @y , 16 ) + dbo.SHA_RShift( @lsw , 16 ) RETURN   dbo.SHA_LShift( @msw , 16 |  ( @lsw & 0xFFFF ) END     GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO /***********************************************************************************// The standard SHA1 needs the input string to fit into a block// This function align the input string to meet the requirementfunction AlignSHA1(sIn){  var nblk=((sIn.length+8)>>6)+1, blks=new Array(nblk*16);  for(var i=0;i<nblk*16;i++)blks[i]=0;  for(i=0;i<sIn.length;i++)    blks[i>>2]|=sIn.charCodeAt(i)<<(24-(i&3)*8);  blks[i>>2]|=0x80<<(24-(i&3)*8);  blks[nblk*16-1]=sIn.length*8;  return blks;}************************************************************************************/ CREATE   FUNCTION  dbo.SHA_AlignSHA1( @sIn   varchar ( 8000 )) RETURNS   @tWordArray   TABLE ( [ ID ]   int  , [ Word ]   bigint AS    BEGIN   declare   @nblk   bigint , @i   int set   @nblk = dbo.SHA_RShift(( len ( @sIn ) + 8 ), 6 ) + 1 set   @i = 0 while   @i < @nblk * 16 begin INSERT   into   @tWordArray (ID,Word)  VALUES ( @i , 0 ) set   @i = @i + 1 end set   @i = 0 while   @i < len ( @sIn ) begin   update   @tWordArray   set  Word =  dbo.SHA_LShift( ascii ( substring ( @sIn , @i + 1 , 1 )),( 24 - ( @i & 3 ) * 8 ))  where  id = dbo.SHA_RShift( @i , 2 set   @i = @i + 1 end update   @tWordArray   set   Word = dbo.SHA_LShift( 0x80 ,( 24 - ( @i & 3 ) * 8 ))  where  id = dbo.SHA_RShift( @i , 2 ) update   @tWordArray   set   Word = len ( @sIn ) * 8     where  id = ( @nblk * 16 - 1 ) return   END     GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO /*****************************************************************************// Perform the appropriate triplet combination function for the current roundfunction ft(t,b,c,d){  if(t<20)return(b&c)|((~b)&d);  if(t<40)return b^c^d;  if(t<60)return(b&c)|(b&d)|(c&d);  return b^c^d;}*****************************************************************************/ CREATE   FUNCTION  dbo.SHA_Ft(      @a          bigint      -- -     , @b      bigint ,    -- --       @c          bigint      -- -     , @d      bigint     -- -- ) RETURNS   bigint AS BEGIN declare   @v   bigint set   @v = 0 if   @a < 20 begin set   @v = ( @b & @c ) | (( ~ @b ) & @d ) end if   @a >= 20   and   @a < 40   begin set   @v =   @b ^ @c ^ @d end if   @a >= 40   and   @a < 60   begin set   @v =  ( @b & @c ) | ( @b & @d ) | ( @c & @d ) end RETURN    @v END    GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO /*****************************************************************************2进制散列转换function hex(num){  var str="";  for(var j=7;j>=0;j--)    str+=sHEXChars.charAt((num>>(j*4))&0x0F);  return str;}*****************************************************************************/ CREATE   FUNCTION  dbo.SHA_Hex( @iValue     bigint ) RETURNS    char ( 8 ) AS BEGIN      DECLARE   @iRes   BIGINT , @str   varchar ( 8 ), @Len   int set   @iRes = 8 set   @str = ''   while   @iRes > 0 begin set   @Len = dbo.SHA_RShift( @iValue ,( @iRes - 1 ) * 4 ) & 0x0F set   @str = @str + substring ( ' 0123456789abcdef ' , @Len + 1 , 1 ) set   @iRes = @iRes - 1 end RETURN    @str END   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO /*****************************************************************************function kt(t) {  return(t<20)?1518500249:(t<40)?1859775393:    (t<60)?-1894007588:-899497514;}*****************************************************************************/ CREATE   FUNCTION  dbo.SHA_Kt( @iValue     bigint ) RETURNS   bigint AS BEGIN DECLARE   @v   bigint if  ( @iValue < 20 set   @v = 1518500249 if  ( @iValue < 40    and   @iValue >= 20 set   @v = 1859775393 if  ( @iValue < 60   and   @iValue >= 40 set   @v = 1894007588 if  ( @iValue >= 60 set   @v =- 899497514 RETURN    @v END    GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO -- -左移位 CREATE   FUNCTION  dbo.SHA_LShift(      @iValue          BIGINT     , @iShiftBits      TINYINT ) RETURNS   bigint AS BEGIN      DECLARE   @iRes      BIGINT      SET   @iRes   =   CAST ( @iValue   AS   BINARY ( 8 ))     SET   @iRes   =   @iRes   *  dbo.SHA_Power( @iShiftBits )     RETURN ( CAST ( @iRes   &   0x00000000FFFFFFFF   AS   BINARY ( 4 ))) END     GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO   /****************************************************************************** Name: SHA_m_2Power* Description: 常数组*****************************************************************************/ CREATE   FUNCTION  dbo.SHA_Power(     @i      TINYINT ) RETURNS   bigint -- WITH ENCRYPTION AS BEGIN      DECLARE   @iRes      bigint      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             WHEN   31   THEN   2147483648   --  10000000000000000000000000000000              ELSE   0          END      RETURN ( @iRes ) END    GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO /*****************************************************************************无符号向右移位*****************************************************************************/ CREATE   FUNCTION  dbo.SHA_R2Shift(      @iValue          BIGINT      -- -要移位的数字     , @iShiftBits      int     -- --移位得位数 ) RETURNS   bigint AS BEGIN      DECLARE   @iRes      BIGINT      SET   @iRes   =   CAST ( @iValue   AS   BINARY ( 8 )) if   @iValue >= 0 begin   SET   @iRes   =   CAST ( @iValue   AS   BINARY ( 8 ))  SET   @iRes   =   @iRes   /  dbo. SHA_Power( @iShiftBits ) end else begin   SET   @iRes   =   CAST (( 0 - @iValue AS   BINARY ( 8 ))       SET   @iRes   = dbo. SHA_Power( 32 - @iShiftBits ) - @iRes   / dbo. SHA_Power( @iShiftBits ) - 1 end      RETURN ( CAST ( @iRes   &   0x00000000FFFFFFFF   AS   BINARY ( 4 ))) END      GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO /*****************************************************************************向右移位*****************************************************************************/ CREATE   FUNCTION  dbo.SHA_RShift(      @iValue          BIGINT      -- -要移位的数字     , @iShiftBits      TINYINT     -- --移位得位数 ) RETURNS   bigint AS BEGIN      DECLARE   @iRes      BIGINT      SET   @iRes   =   CAST ( @iValue   AS   BINARY ( 8 ))     SET   @iRes   =   @iRes   /  dbo.SHA_Power( @iShiftBits )     RETURN ( CAST ( @iRes   &   0x00000000FFFFFFFF   AS   BINARY ( 4 ))) END    GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO /*****************************************************************************// The int32 _asm rol :)function rol(num,cnt){  return(num<<cnt)|(num>>>(32-cnt));}*****************************************************************************/ CREATE   FUNCTION  dbo.SHA_Rol(      @x          bigint      -- -要移位的数字     , @y      bigint     -- --移位得位数 ) RETURNS   bigint AS BEGIN RETURN   dbo.SHA_LShift( @x , @y ) |  dbo.SHA_R2Shift( @x , 32 - @y ) END   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO /*---------------------------------------------------------------------------------以下为测试脚本海风编写功能:批量生成随机密码 和加密密码进行输出参数:@strlen密码长度@scnt   密码数量@cs 数据 加密串*/ CREATE   PROCEDURE   [ RndPWD ] @strlen   int , @scnt   int , @cs   varchar ( 10 AS set  nocount  on CREATE   TABLE   [ #rndpwd_hch ]  (  [ PassWord ]   [ varchar ]  ( 20 ) COLLATE Chinese_PRC_CI_AS  NOT   NULL  , ) declare   @Cnt   int , @chvRand   varchar ( 20 ) set   @chvRand = '' set   @Cnt = 0 while ( @Cnt < @scnt ) begin    set   @chvRand   = substring ( cast ( rand ()  as   varchar ), 3 , 4 ) +   substring ( cast ( rand ()  as   varchar ), 3 , 4 ) +   substring ( cast ( rand ()  as   varchar ), 3 , 4 set   @Cnt = @Cnt + 1   insert   into  #rndpwd_hch(PassWord)  values ( substring ( cast ( cast ( @chvRand   as   bigint as   varchar ( 30 )), 1 , @strlen )) end select    [ Password ] ,dbo.SHA1Password + @cs as  PassWordSHA    from  #rndpwd_hch drop   table  #rndpwd_hch GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  ON   GO SET  QUOTED_IDENTIFIER  OFF   GO SET  ANSI_NULLS  OFF   GO    

    最新回复(0)