首页 > 代码 > 常规代码 > 正文

代码

阅读排行

MD5 哈希值计算程序
2014-01-20 20:23:01   来源:Fcode研讨团队   评论:0 点击:

本程序可根据字符串值计算其MD5哈希值,完全符合语法规范,可直接使用。

如下代码,本例中,计算字符串“www.fcode.cn”的MD5值为 “807ED3F7ACD8AA755EE710BB641FA1B2”
Module MD5_Mod
  Implicit None
  Private umdrehen , leftrotate
contains
  Subroutine md5( cStr , h0 , h1 , h2 , h3 )            
    implicit none          
    Character(Len=*) , Intent( IN ) :: cStr
    Integer , Intent( OUT) :: h0,h1,h2,h3
    character( Len=(int(len(cStr)/64)+1)*64 ) :: newString   
    character( Len=8 ) :: wtmp    
    integer(kind=4) j,n1,n2,n3,n4,pos   
    integer(kind=4) r(64),k(64),a,b,c,d,f,g,temp,w(16),i,intLen   
    integer(kind=8) hoch32   
    real(kind=8) sinus,absolut,real8i          
    r = (/7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22, 5,  9, 14, &
      20  ,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20, 4, 11, 16, 23,  4, 11, 16, 23,&
      4   , 11, 16, 23,  4, 11, 16, 23, 6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21/)       
    do i=1,64   
      real8i = 1.0_8 * int(i,Kind=8)
      sinus = dsin(real8i)   
      absolut = dabs(sinus)   
      hoch32 = 2.**32.   
      k(i) = int( absolut * hoch32 , Kind = 8 )   
    end do          
    h0 = 1732584193 !#67452301   
    h1 = -271733879 !#EFCDAB89   
    h2 = -1732584194 !#98BADCFE   
    h3 = 271733878  !#10325476          
    j = len(cStr)+1   
    newString(:j) = cStr // char(128)   
    i = mod(j, 64)   
    do while(i /= 56)   
      j = j + 1   
      newString(j:j) = char(0)   
      i = mod(j, 64)   
    end do          
    intLen = len(cStr)*8   
    do i = 0,3   
      temp = iand(intLen, 255 )
      j = j + 1   
      newString(j:j) = char(temp)   
      intLen = ISHFT(intLen, -8)
    end do          
    do i = 1,4   
      j = j + 1   
      newString(j:j) = char(0)   
    end do          
    do i = 1,int(len(newString)/64)          
      do j = 1,16   
        pos = (j-1)*4+(i-1)*64   
        n1 = ichar(newString(4+pos:4+pos))   
        n2 = ichar(newString(3+pos:3+pos))   
        n3 = ichar(newString(2+pos:2+pos))   
        n4 = ichar(newString(1+pos:1+pos))
        write(wtmp,'(4(z2.2))') n1,n2,n3,n4   
        read(wtmp,'(z8)') w(j)   
      end do          
      a = h0   
      b = h1   
      c = h2   
      d = h3          
      do j = 1,64   
        if (j >= 1 .and. j <= 16) then   
          f = ior( iand(b,c) , iand(not(b), d)  )
          g = j   
        else if (j >= 17 .and. j <= 32) then   
          f = ior( iand(d , b) , iand(not(d) , c)   )
          g = mod(5*(j-1) + 1, 16) + 1   
        else if (j >= 33 .and. j <= 48) then   
          f = ieor(b, ieor(c, d))   
          g = mod(3*(j-1) + 5, 16) + 1   
        else if (j >= 49 .and. j <= 64) then   
          f = ieor(c, ior(b , not(d)))   
          g = mod(7*(j-1), 16) + 1   
        end if                  
        temp = d   
        d = c   
        c = b   
        b = b + leftrotate((a + f + k(j) + w(g)) , r(j))   
        a = temp   
      end do          
      h0 = h0 + a   
      h1 = h1 + b   
      h2 = h2 + c   
      h3 = h3 + d   
    end do   
    h0 = umdrehen(h0)   
    h1 = umdrehen(h1)   
    h2 = umdrehen(h2)   
    h3 = umdrehen(h3)          
    return        
  End Subroutine md5   
  
  Integer(kind=4) function leftrotate (x, c)   
    integer(kind=4) x,c,result1,result2        
    result1 = ishft(x,c)   
    result2 = ishft(x, (c-32))        
    leftrotate = ior( result1 , result2 )
    return   
  End function leftrotate     
    
  Integer(kind=4) function umdrehen(zahl)   
    integer(kind=4) i,tmp,zahl        
    umdrehen = 0   
    do i = 1,4   
      umdrehen = ishft(umdrehen, 8)   
      tmp = iand( zahl , 255 )
      umdrehen = umdrehen + tmp;   
      zahl = ishft(zahl, -8)
    end do        
    return   
  End function umdrehen  
  
End Module MD5_Mod  

Program www_fcode_cn
  use MD5_Mod
  Implicit None
  Integer :: i1,i2,i3,i4
  call MD5("www.fcode.cn",i1,i2,i3,i4)
  write(*,'(4z8)') i1,i2,i3,i4
End Program www_fcode_cn

相关热词搜索:MD5 哈希值

上一篇:文件夹内循环读取文件[仅适用于VF编译器]
下一篇:任意表达式求值模块

分享到: 收藏