Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Using SHA1 and MD5 in ASP.NET

3.83/5 (6 votes)
3 Dec 2013CPOL1 min read 23K  
SHA1 and MD5 class for net version.

Introduction

In this article we shall see how C# can use SHA1 or MD5 to calculate a hash algorithm string.

Background

Recently I had a new project to make a payment online which involved a secure hash algorithm.

In the past I used the System.Security.Cryptography library for encrypting or decrypting a hash string, not worrying how the SHA1 or MD5 algorithms worked. But now in my project I have to call a SHA1 or MD5 function based on the ASP page to calculate the hash string.

I searched on the internet to find a way to call ASP functions from the Net version but no luck. So I decided to create a class to expose SHA1 or MD5 functions for the Net version and I posted here thinking maybe somebody is interested.

Using the code

The attached code uses VB.NET 2008. It has:

  • VB.NET syntax

The break down

There are two public functions which can be called from outside of the class. Just create a new library using VB.NET, copy and paste the code into your project, and compile it into a DLL file. Then call SHA1 or MD5 functions from C# or VB.NET.

VB
Public Class Utils
    Private Const BITS_TO_A_BYTE As Integer = 8
    Private Const BYTES_TO_A_WORD As Integer = 4
    Private Const BITS_TO_A_WORD As Integer = 32
    Private Shared m_lOnBits(30) As Integer
    Private Shared m_l2Power(30) As Integer

    Private Shared Function AndW(ByRef pBytWord1Ary() As Integer, ByRef pBytWord2Ary() As Integer) As Integer()
        Dim lBytWordAry(3) As Integer
        Dim lLngIndex As Integer
        For lLngIndex = 0 To 3
            lBytWordAry(lLngIndex) = CByte(pBytWord1Ary(lLngIndex) And pBytWord2Ary(lLngIndex))
        Next
        AndW = lBytWordAry
    End Function
    Private Shared Function OrW(ByRef pBytWord1Ary() As Integer, ByRef pBytWord2Ary() As Integer) As Integer()
        Dim lBytWordAry(3) As Integer
        Dim lLngIndex As Integer
        For lLngIndex = 0 To 3
            lBytWordAry(lLngIndex) = CByte(pBytWord1Ary(lLngIndex) Or pBytWord2Ary(lLngIndex))
        Next
        OrW = lBytWordAry
    End Function
    Private Shared Function XorW(ByRef pBytWord1Ary() As Integer, ByRef pBytWord2Ary() As Integer) As Integer()
        Dim lBytWordAry(3) As Integer
        Dim lLngIndex As Integer
        For lLngIndex = 0 To 3
            lBytWordAry(lLngIndex) = CByte(pBytWord1Ary(lLngIndex) Xor pBytWord2Ary(lLngIndex))
        Next
        XorW = lBytWordAry
    End Function
    Private Shared Function NotW(ByRef pBytWordAry() As Integer) As Integer()
        Dim lBytWordAry(3) As Integer
        Dim lLngIndex As Integer
        For lLngIndex = 0 To 3
            lBytWordAry(lLngIndex) = Not CByte(pBytWordAry(lLngIndex))
        Next
        NotW = lBytWordAry
    End Function
    Private Shared Function AddW(ByRef pBytWord1Ary() As Integer, ByRef pBytWord2Ary() As Integer) As Integer()
        Dim lLngIndex As Integer
        Dim lIntTotal As Integer
        Dim lBytWordAry(3) As Integer
        For lLngIndex = 3 To 0 Step -1
            If lLngIndex = 3 Then
                lIntTotal = CShort(pBytWord1Ary(lLngIndex)) + pBytWord2Ary(lLngIndex)
                lBytWordAry(lLngIndex) = lIntTotal Mod 256
            Else
                lIntTotal = CShort(pBytWord1Ary(lLngIndex)) + pBytWord2Ary(lLngIndex) + (lIntTotal \ 256)
                lBytWordAry(lLngIndex) = lIntTotal Mod 256
            End If
        Next
        AddW = lBytWordAry.Clone()
    End Function
    Private Shared Function CircShiftLeftW(ByRef pBytWordAry() As Integer, ByRef pLngShift As Integer) As Integer()
        Dim lDbl1 As Double
        Dim lDbl2 As Double
        lDbl1 = WordToDouble(pBytWordAry)
        lDbl2 = lDbl1
        lDbl1 = CDbl(lDbl1 * (2 ^ pLngShift))
        lDbl2 = CDbl(lDbl2 / (2 ^ (32 - pLngShift)))
        CircShiftLeftW = OrW(DoubleToWord(lDbl1), DoubleToWord(lDbl2))
    End Function
    Private Shared Function WordToHex(ByRef pBytWordAry() As Integer) As String
        WordToHex = ""
        Dim lLngIndex As Integer
        For lLngIndex = 0 To 3
            WordToHex = WordToHex & Right("0" & Hex(pBytWordAry(lLngIndex)), 2)
        Next
    End Function
    Private Shared Function HexToWord(ByRef pStrHex As String) As Integer()
        HexToWord = DoubleToWord(CDbl("&h" & pStrHex)) ' needs "#" at end for VB?
    End Function
    Private Shared Function DoubleToWord(ByRef pDblValue As Double) As Integer()
        Dim lBytWordAry(3) As Integer
        lBytWordAry(0) = Int(DMod(pDblValue, 2 ^ 32) / (2 ^ 24))
        lBytWordAry(1) = Int(DMod(pDblValue, 2 ^ 24) / (2 ^ 16))
        lBytWordAry(2) = Int(DMod(pDblValue, 2 ^ 16) / (2 ^ 8))
        lBytWordAry(3) = Int(DMod(pDblValue, 2 ^ 8))
        DoubleToWord = lBytWordAry.Clone()
    End Function
    Private Shared Function WordToDouble(ByRef pBytWordAry() As Integer) As Double
        WordToDouble = CDbl((pBytWordAry(0) * (2 ^ 24)) + (pBytWordAry(1) * _
          (2 ^ 16)) + (pBytWordAry(2) * (2 ^ 8)) + pBytWordAry(3))
    End Function
    Private Shared Function DMod(ByRef pDblValue As Double, ByRef pDblDivisor As Double) As Double
        Dim lDblMod As Double
        lDblMod = CDbl(CDbl(pDblValue) - (Int(CDbl(pDblValue) / CDbl(pDblDivisor)) * CDbl(pDblDivisor)))
        If lDblMod < 0 Then
            lDblMod = CDbl(lDblMod + pDblDivisor)
        End If
        DMod = lDblMod
    End Function
    Private Shared Function F(ByRef lIntT As Integer, ByRef pBytWordBAry() As Integer, _
      ByRef pBytWordCAry() As Integer, ByRef pBytWordDAry() As Integer) As Integer()
        If lIntT <= 19 Then
            F = OrW(AndW(pBytWordBAry, pBytWordCAry), AndW(NotW(pBytWordBAry), pBytWordDAry))
        ElseIf lIntT <= 39 Then
            F = XorW(XorW(pBytWordBAry, pBytWordCAry), pBytWordDAry)
        ElseIf lIntT <= 59 Then
            F = OrW(OrW(AndW(pBytWordBAry, pBytWordCAry), _
                  AndW(pBytWordBAry, pBytWordDAry)), AndW(pBytWordCAry, pBytWordDAry))
        Else
            F = XorW(XorW(pBytWordBAry, pBytWordCAry), pBytWordDAry)
        End If
    End Function
    Public Shared Function calcSHA1(ByVal pStrMessage As String) As String
        Dim lLngLen As Integer
        Dim lBytLenW() As Integer
        Dim lStrPadMessage As String
        Dim lLngNumBlocks As Integer
        Dim lVarWordKAry(3)() As Integer
        Dim lBytWordH0Ary() As Integer
        Dim lBytWordH1Ary() As Integer
        Dim lBytWordH2Ary() As Integer
        Dim lBytWordH3Ary() As Integer
        Dim lBytWordH4Ary() As Integer
        Dim lLngBlock As Integer
        Dim lStrBlockText As String
        Dim lIntT As Integer
        Dim lStrWordText As String
        Dim lVarWordWAry(79)() As Integer
        Dim lBytWordAAry() As Integer
        Dim lBytWordBAry() As Integer
        Dim lBytWordCAry() As Integer
        Dim lBytWordDAry() As Integer
        Dim lBytWordEAry() As Integer
        Dim lBytWordFAry() As Integer
        Dim lBytTempAry() As Integer

        lLngLen = Len(pStrMessage)
        lBytLenW = DoubleToWord(CDbl(lLngLen) * 8)
        lStrPadMessage = pStrMessage & Chr(128) & New String(Chr(0), _
          (128 - (lLngLen Mod 64) - 9) Mod 64) & New String(Chr(0), 4) & _
          Chr(lBytLenW(0)) & Chr(lBytLenW(1)) & Chr(lBytLenW(2)) & Chr(lBytLenW(3))
        lLngNumBlocks = Len(lStrPadMessage) / 64
        lVarWordKAry(0) = HexToWord("5A827999")
        lVarWordKAry(1) = HexToWord("6ED9EBA1")
        lVarWordKAry(2) = HexToWord("8F1BBCDC")
        lVarWordKAry(3) = HexToWord("CA62C1D6")
        lBytWordH0Ary = HexToWord("67452301")
        lBytWordH1Ary = HexToWord("EFCDAB89")
        lBytWordH2Ary = HexToWord("98BADCFE")
        lBytWordH3Ary = HexToWord("10325476")
        lBytWordH4Ary = HexToWord("C3D2E1F0")
        For lLngBlock = 0 To lLngNumBlocks - 1
            lStrBlockText = Mid(lStrPadMessage, (lLngBlock * 64) + 1, 64)
            For lIntT = 0 To 15
                lStrWordText = Mid(lStrBlockText, (lIntT * 4) + 1, 4)
                lVarWordWAry(lIntT) = New Integer() {Asc(Mid(lStrWordText, 1, 1)), _
                   Asc(Mid(lStrWordText, 2, 1)), Asc(Mid(lStrWordText, 3, 1)), _
                   Asc(Mid(lStrWordText, 4, 1))}
            Next
            For lIntT = 16 To 79
                lVarWordWAry(lIntT) = CircShiftLeftW(XorW(XorW(XorW(lVarWordWAry(lIntT - 3), _
                  lVarWordWAry(lIntT - 8)), lVarWordWAry(lIntT - 14)), lVarWordWAry(lIntT - 16)), 1)
            Next
            lBytWordAAry = lBytWordH0Ary.Clone()
            lBytWordBAry = lBytWordH1Ary.Clone()
            lBytWordCAry = lBytWordH2Ary.Clone()
            lBytWordDAry = lBytWordH3Ary.Clone()
            lBytWordEAry = lBytWordH4Ary.Clone()
            For lIntT = 0 To 79
                lBytWordFAry = F(lIntT, lBytWordBAry, lBytWordCAry, lBytWordDAry)
                lBytTempAry = AddW(AddW(AddW(AddW(CircShiftLeftW(lBytWordAAry, _
                  5), lBytWordFAry), lBytWordEAry), lVarWordWAry(lIntT)), lVarWordKAry(lIntT \ 20))
                lBytWordEAry = lBytWordDAry.Clone()
                lBytWordDAry = lBytWordCAry.Clone()
                lBytWordCAry = CircShiftLeftW(lBytWordBAry, 30)
                lBytWordBAry = lBytWordAAry.Clone()
                lBytWordAAry = lBytTempAry.Clone()
            Next
            lBytWordH0Ary = AddW(lBytWordH0Ary, lBytWordAAry)
            lBytWordH1Ary = AddW(lBytWordH1Ary, lBytWordBAry)
            lBytWordH2Ary = AddW(lBytWordH2Ary, lBytWordCAry)
            lBytWordH3Ary = AddW(lBytWordH3Ary, lBytWordDAry)
            lBytWordH4Ary = AddW(lBytWordH4Ary, lBytWordEAry)
        Next
        calcSHA1 = WordToHex(lBytWordH0Ary) & WordToHex(lBytWordH1Ary) & _
           WordToHex(lBytWordH2Ary) & WordToHex(lBytWordH3Ary) & WordToHex(lBytWordH4Ary)
        calcSHA1 = LCase(calcSHA1)
    End Function

    Private Shared Function LShift(ByVal lValue As Integer, ByVal iShiftBits As Integer) As Integer
        If iShiftBits = 0 Then
            LShift = lValue
            Exit Function
        ElseIf iShiftBits = 31 Then
            If lValue And 1 Then
                LShift = &H80000000
            Else
                LShift = 0
            End If
            Exit Function
        ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
            Err.Raise(6)
        End If

        If (lValue And m_l2Power(31 - iShiftBits)) Then
            LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
        Else
            LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
        End If
    End Function
    Private Shared Function RShift(ByVal lValue As Integer, ByVal iShiftBits As Integer) As Integer
        If iShiftBits = 0 Then
            RShift = lValue
            Exit Function
        ElseIf iShiftBits = 31 Then
            If lValue And &H80000000 Then
                RShift = 1
            Else
                RShift = 0
            End If
            Exit Function
        ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
            Err.Raise(6)
        End If

        RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)

        If (lValue And &H80000000) Then
            RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
        End If
    End Function
    Private Shared Function RotateLeft(ByVal lValue As Integer, ByVal iShiftBits As Integer) As Integer
        RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
    End Function
    Private Shared Function AddUnsigned(ByVal lX As Integer, ByVal lY As Integer) As Integer
        Dim lX4 As Integer
        Dim lY4 As Integer
        Dim lX8 As Integer
        Dim lY8 As Integer
        Dim lResult As Integer

        lX8 = lX And &H80000000
        lY8 = lY And &H80000000
        lX4 = lX And &H40000000
        lY4 = lY And &H40000000

        lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)

        If lX4 And lY4 Then
            lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
        ElseIf lX4 Or lY4 Then
            If lResult And &H40000000 Then
                lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
            Else
                lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
            End If
        Else
            lResult = lResult Xor lX8 Xor lY8
        End If

        AddUnsigned = lResult
    End Function
    Private Shared Function F(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
        F = (x And y) Or ((Not x) And z)
    End Function
    Private Shared Function G(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
        G = (x And z) Or (y And (Not z))
    End Function
    Private Shared Function H(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
        H = (x Xor y Xor z)
    End Function
    Private Shared Function I(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
        I = (y Xor (x Or (Not z)))
    End Function
    Private Shared Function FF(ByVal a As Integer, ByVal b As Integer, _
       ByVal c As Integer, ByVal d As Integer, ByVal x As Integer, _
       ByVal s As Integer, ByVal ac As Integer) As Integer
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac))
        a = RotateLeft(a, s)
        a = AddUnsigned(a, b)
        FF = a
    End Function
    Private Shared Function GG(ByVal a As Integer, ByVal b As Integer, _
      ByVal c As Integer, ByVal d As Integer, ByVal x As Integer, _
      ByVal s As Integer, ByVal ac As Integer) As Integer
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac))
        a = RotateLeft(a, s)
        a = AddUnsigned(a, b)
        GG = a
    End Function
    Private Shared Function HH(ByVal a As Integer, ByVal b As Integer, _
      ByVal c As Integer, ByVal d As Integer, ByVal x As Integer, _
      ByVal s As Integer, ByVal ac As Integer) As Integer
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac))
        a = RotateLeft(a, s)
        a = AddUnsigned(a, b)
        HH = a
    End Function
    Private Shared Function II(ByVal a As Integer, ByVal b As Integer, _
      ByVal c As Integer, ByVal d As Integer, ByVal x As Integer, _
      ByVal s As Integer, ByVal ac As Integer) As Integer
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac))
        a = RotateLeft(a, s)
        a = AddUnsigned(a, b)
        II = a
    End Function
    Private Shared Function ConvertToWordArray(ByVal sMessage As String) As Integer()
        Dim lMessageLength As Integer
        Dim lNumberOfWords As Integer
        Dim lWordArray() As Integer
        Dim lBytePosition As Integer
        Dim lByteCount As Integer
        Dim lWordCount As Integer

        Const MODULUS_BITS As Integer = 512
        Const CONGRUENT_BITS As Integer = 448

        lMessageLength = Len(sMessage)

        lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ _
          BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
        ReDim lWordArray(lNumberOfWords - 1)

        lBytePosition = 0
        lByteCount = 0
        Do Until lByteCount >= lMessageLength
            lWordCount = lByteCount \ BYTES_TO_A_WORD
            lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
            lWordArray(lWordCount) = lWordArray(lWordCount) Or _
              LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition)
            lByteCount = lByteCount + 1
        Loop

        lWordCount = lByteCount \ BYTES_TO_A_WORD
        lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE

        lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)

        lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
        lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)

        ConvertToWordArray = lWordArray
    End Function
    Private Shared Function WordToHex(ByVal lValue As Integer) As String
        WordToHex = ""
        Dim lByte As Integer
        Dim lCount As Integer
        For lCount = 0 To 3
            lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
            WordToHex = WordToHex & Right("0" & Hex(lByte), 2)
        Next
    End Function
    Public Shared Function MD5(ByVal sMessage As String) As String
        Dim x As Integer()
        Dim k As Integer
        Dim AA As Integer
        Dim BB As Integer
        Dim CC As Integer
        Dim DD As Integer
        Dim a As Integer
        Dim b As Integer
        Dim c As Integer
        Dim d As Integer

        Const S11 As Integer = 7
        Const S12 As Integer = 12
        Const S13 As Integer = 17
        Const S14 As Integer = 22
        Const S21 As Integer = 5
        Const S22 As Integer = 9
        Const S23 As Integer = 14
        Const S24 As Integer = 20
        Const S31 As Integer = 4
        Const S32 As Integer = 11
        Const S33 As Integer = 16
        Const S34 As Integer = 23
        Const S41 As Integer = 6
        Const S42 As Integer = 10
        Const S43 As Integer = 15
        Const S44 As Integer = 21

        m_lOnBits(0) = 1
        m_lOnBits(1) = 3
        m_lOnBits(2) = 7
        m_lOnBits(3) = 15
        m_lOnBits(4) = 31
        m_lOnBits(5) = 63
        m_lOnBits(6) = 127
        m_lOnBits(7) = 255
        m_lOnBits(8) = 511
        m_lOnBits(9) = 1023
        m_lOnBits(10) = 2047
        m_lOnBits(11) = 4095
        m_lOnBits(12) = 8191
        m_lOnBits(13) = 16383
        m_lOnBits(14) = 32767
        m_lOnBits(15) = 65535
        m_lOnBits(16) = 131071
        m_lOnBits(17) = 262143
        m_lOnBits(18) = 524287
        m_lOnBits(19) = 1048575
        m_lOnBits(20) = 2097151
        m_lOnBits(21) = 4194303
        m_lOnBits(22) = 8388607
        m_lOnBits(23) = 16777215
        m_lOnBits(24) = 33554431
        m_lOnBits(25) = 67108863
        m_lOnBits(26) = 134217727
        m_lOnBits(27) = 268435455
        m_lOnBits(28) = 536870911
        m_lOnBits(29) = 1073741823
        m_lOnBits(30) = 2147483647

        m_l2Power(0) = 1
        m_l2Power(1) = 2
        m_l2Power(2) = 4
        m_l2Power(3) = 8
        m_l2Power(4) = 16
        m_l2Power(5) = 32
        m_l2Power(6) = 64
        m_l2Power(7) = 128
        m_l2Power(8) = 256
        m_l2Power(9) = 512
        m_l2Power(10) = 1024
        m_l2Power(11) = 2048
        m_l2Power(12) = 4096
        m_l2Power(13) = 8192
        m_l2Power(14) = 16384
        m_l2Power(15) = 32768
        m_l2Power(16) = 65536
        m_l2Power(17) = 131072
        m_l2Power(18) = 262144
        m_l2Power(19) = 524288
        m_l2Power(20) = 1048576
        m_l2Power(21) = 2097152
        m_l2Power(22) = 4194304
        m_l2Power(23) = 8388608
        m_l2Power(24) = 16777216
        m_l2Power(25) = 33554432
        m_l2Power(26) = 67108864
        m_l2Power(27) = 134217728
        m_l2Power(28) = 268435456
        m_l2Power(29) = 536870912
        m_l2Power(30) = 1073741824

        x = ConvertToWordArray(sMessage)

        a = &H67452301
        b = &HEFCDAB89
        c = &H98BADCFE
        d = &H10325476

        For k = 0 To UBound(x) Step 16
            AA = a
            BB = b
            CC = c
            DD = d

            a = FF(a, b, c, d, x(k + 0), S11, &HD76AA478)
            d = FF(d, a, b, c, x(k + 1), S12, &HE8C7B756)
            c = FF(c, d, a, b, x(k + 2), S13, &H242070DB)
            b = FF(b, c, d, a, x(k + 3), S14, &HC1BDCEEE)
            a = FF(a, b, c, d, x(k + 4), S11, &HF57C0FAF)
            d = FF(d, a, b, c, x(k + 5), S12, &H4787C62A)
            c = FF(c, d, a, b, x(k + 6), S13, &HA8304613)
            b = FF(b, c, d, a, x(k + 7), S14, &HFD469501)
            a = FF(a, b, c, d, x(k + 8), S11, &H698098D8)
            d = FF(d, a, b, c, x(k + 9), S12, &H8B44F7AF)
            c = FF(c, d, a, b, x(k + 10), S13, &HFFFF5BB1)
            b = FF(b, c, d, a, x(k + 11), S14, &H895CD7BE)
            a = FF(a, b, c, d, x(k + 12), S11, &H6B901122)
            d = FF(d, a, b, c, x(k + 13), S12, &HFD987193)
            c = FF(c, d, a, b, x(k + 14), S13, &HA679438E)
            b = FF(b, c, d, a, x(k + 15), S14, &H49B40821)

            a = GG(a, b, c, d, x(k + 1), S21, &HF61E2562)
            d = GG(d, a, b, c, x(k + 6), S22, &HC040B340)
            c = GG(c, d, a, b, x(k + 11), S23, &H265E5A51)
            b = GG(b, c, d, a, x(k + 0), S24, &HE9B6C7AA)
            a = GG(a, b, c, d, x(k + 5), S21, &HD62F105D)
            d = GG(d, a, b, c, x(k + 10), S22, &H2441453)
            c = GG(c, d, a, b, x(k + 15), S23, &HD8A1E681)
            b = GG(b, c, d, a, x(k + 4), S24, &HE7D3FBC8)
            a = GG(a, b, c, d, x(k + 9), S21, &H21E1CDE6)
            d = GG(d, a, b, c, x(k + 14), S22, &HC33707D6)
            c = GG(c, d, a, b, x(k + 3), S23, &HF4D50D87)
            b = GG(b, c, d, a, x(k + 8), S24, &H455A14ED)
            a = GG(a, b, c, d, x(k + 13), S21, &HA9E3E905)
            d = GG(d, a, b, c, x(k + 2), S22, &HFCEFA3F8)
            c = GG(c, d, a, b, x(k + 7), S23, &H676F02D9)
            b = GG(b, c, d, a, x(k + 12), S24, &H8D2A4C8A)

            a = HH(a, b, c, d, x(k + 5), S31, &HFFFA3942)
            d = HH(d, a, b, c, x(k + 8), S32, &H8771F681)
            c = HH(c, d, a, b, x(k + 11), S33, &H6D9D6122)
            b = HH(b, c, d, a, x(k + 14), S34, &HFDE5380C)
            a = HH(a, b, c, d, x(k + 1), S31, &HA4BEEA44)
            d = HH(d, a, b, c, x(k + 4), S32, &H4BDECFA9)
            c = HH(c, d, a, b, x(k + 7), S33, &HF6BB4B60)
            b = HH(b, c, d, a, x(k + 10), S34, &HBEBFBC70)
            a = HH(a, b, c, d, x(k + 13), S31, &H289B7EC6)
            d = HH(d, a, b, c, x(k + 0), S32, &HEAA127FA)
            c = HH(c, d, a, b, x(k + 3), S33, &HD4EF3085)
            b = HH(b, c, d, a, x(k + 6), S34, &H4881D05)
            a = HH(a, b, c, d, x(k + 9), S31, &HD9D4D039)
            d = HH(d, a, b, c, x(k + 12), S32, &HE6DB99E5)
            c = HH(c, d, a, b, x(k + 15), S33, &H1FA27CF8)
            b = HH(b, c, d, a, x(k + 2), S34, &HC4AC5665)

            a = II(a, b, c, d, x(k + 0), S41, &HF4292244)
            d = II(d, a, b, c, x(k + 7), S42, &H432AFF97)
            c = II(c, d, a, b, x(k + 14), S43, &HAB9423A7)
            b = II(b, c, d, a, x(k + 5), S44, &HFC93A039)
            a = II(a, b, c, d, x(k + 12), S41, &H655B59C3)
            d = II(d, a, b, c, x(k + 3), S42, &H8F0CCC92)
            c = II(c, d, a, b, x(k + 10), S43, &HFFEFF47D)
            b = II(b, c, d, a, x(k + 1), S44, &H85845DD1)
            a = II(a, b, c, d, x(k + 8), S41, &H6FA87E4F)
            d = II(d, a, b, c, x(k + 15), S42, &HFE2CE6E0)
            c = II(c, d, a, b, x(k + 6), S43, &HA3014314)
            b = II(b, c, d, a, x(k + 13), S44, &H4E0811A1)
            a = II(a, b, c, d, x(k + 4), S41, &HF7537E82)
            d = II(d, a, b, c, x(k + 11), S42, &HBD3AF235)
            c = II(c, d, a, b, x(k + 2), S43, &H2AD7D2BB)
            b = II(b, c, d, a, x(k + 9), S44, &HEB86D391)

            a = AddUnsigned(a, AA)
            b = AddUnsigned(b, BB)
            c = AddUnsigned(c, CC)
            d = AddUnsigned(d, DD)
        Next

        MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))            
    End Function
End Class

History

  • 03 December 2013: First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)