Search code examples
hashvb6sha1

VB6 Hashing SHA1 output not matched


need help for my problem here. i do searching and googling for this problem but still don't found the solution why my output didnt matched with the expected output.

data to hash : 0800210142216688003333311100000554478000000

expected output : DAAC526D4806C88CEDB8B7C6EA42A7442DE6E7DC

my output : 805C790E6BF39E3482067C44909EE126F9CBB878

and i am using this function to generate the hash

Public Function HashString(ByVal Str As String, Optional ByVal Algorithm As HashAlgorithm = SHA1) As String
On Error Resume Next
Dim hCtx As Long
Dim hHash As Long
Dim lRes As Long
Dim lLen As Long
Dim lIdx As Long
Dim AbData() As Byte
lRes = CryptAcquireContext(hCtx, vbNullString, vbNullString, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)
If lRes <> 0 Then
    lRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash)
    If lRes <> 0 Then
        lRes = CryptHashData(hHash, ByVal Str, Len(Str), 0)
        If lRes <> 0 Then
            lRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0)
            If lRes <> 0 Then
                ReDim AbData(0 To lLen - 1)
                lRes = CryptGetHashParam(hHash, HP_HASHVAL, AbData(0), lLen, 0)
                If lRes <> 0 Then
                    For lIdx = 0 To UBound(AbData)
                        HashString = HashString & Right$("0" & Hex$(AbData(lIdx)), 2)
                    Next
                End If
            End If
        End If
        CryptDestroyHash hHash
    End If

End If
CryptReleaseContext hCtx, 0
If lRes = 0 Then
    MsgBox Err.LastDllError
End If
End Function

and this is command to call the function

Dim received As String
Dim HASH As String

HASH = "0800210142216688003333311100000554478000000"

received = HashString(HASH)

Debug.Print ("HASH VALUE : " & received)

thanks

UPDATE:

finally i managed to get the expected output. i change the function to generate the sha1 using the sha1 function in this website : http://vb.wikia.com/wiki/SHA-1.bas

and i do use this function to convert my hexstring to byte array

Public Function HexStringToByteArray(ByRef HexString As String) As Byte()
    Dim bytOut() As Byte, bytHigh As Byte, bytLow As Byte, lngA As Long
    If LenB(HexString) Then
        ' preserve memory for output buffer
        ReDim bytOut(Len(HexString) \ 2 - 1)
        ' jump by every two characters (in this case we happen to use byte positions for greater speed)
        For lngA = 1 To LenB(HexString) Step 4
            ' get the character value and decrease by 48
            bytHigh = AscW(MidB$(HexString, lngA, 2)) - 48
            bytLow = AscW(MidB$(HexString, lngA + 2, 2)) - 48
            ' move old A - F values down even more
            If bytHigh > 9 Then bytHigh = bytHigh - 7
            If bytLow > 9 Then bytLow = bytLow - 7
            ' I guess the C equivalent of this could be like: *bytOut[++i] = (bytHigh << 8) || bytLow
            bytOut(lngA \ 4) = (bytHigh * &H10) Or bytLow
        Next lngA
        ' return the output
        HexStringToByteArray = bytOut
    End If
End Function

and i using this command to get the expected output

Dim received As String
Dim HASH As String
Dim intVal As Integer
Dim temp() As Byte

HASH = "08002101422166880033333111000005544780000000"

temp = HexStringToByteArray(HASH)

received = Replace(HexDefaultSHA1(temp), " ", "")

Debug.Print ("HASH VALUE : " & received)

and finally i got the same output as expected. Yeah!!..


Solution

    • 805c... is the SHA1 hash of the characters in your input string, i.e. '0', '8', '0', '0', ...
    • daac... is the SHA1 hash of the characters in your input string after conversion of each pair of hexadecimal digits to a byte, i.e. 0x08, 0x00, ...

    Convert the input string to an array of bytes prior to hashing.