VB6 散列 SHA1 输出不匹配

VB6 Hashing SHA1 output not matched

这里需要帮助解决我的问题。我正在搜索和谷歌搜索这个问题,但仍然没有找到我的输出与预期输出不匹配的解决方案。

要散列的数据: 0800210142216688003333311100000554478000000

预期输出: DAAC526D4806C88CEDB8B7C6EA42A7442DE6E7DC

我的输出: 805C790E6BF39E3482067C44909EE126F9CBB878

我正在使用此函数生成哈希

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

这是调用函数的命令

Dim received As String
Dim HASH As String

HASH = "0800210142216688003333311100000554478000000"

received = HashString(HASH)

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

谢谢

更新:

我终于得到了预期的输出。我更改了使用本网站中的 sha1 函数生成 sha1 的函数: http://vb.wikia.com/wiki/SHA-1.bas

我确实使用这个函数将我的十六进制字符串转换为字节数组

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

我使用此命令获得预期的输出

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)

最后我得到了与预期相同的输出。是啊!!..

你的输出是正确的。这是 SHA1 使用 python:

>>> import hashlib
>>> s = hashlib.sha1('0800210142216688003333311100000554478000000')
>>> s.hexdigest()
'805c790e6bf39e3482067c44909ee126f9cbb878'

您从哪里获得其他 SHA1 计算?

  • 805c... 是输入字符串中 个字符 的 SHA1 散列,即 '0', '8', '0', '0', ...
  • daac... 是将每对十六进制数字转换为 byte 后输入字符串中字符的 SHA1 哈希值,即 0x08, 0x00, ...

Convert the input string 到散列之前的字节数组。