VB6哈希SHA1输出不匹配

时间:2015-12-16 11:27:49

标签: hash vb6 sha1

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

要散列的数据: 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)

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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

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

你从哪里获得了其他SHA1计算?