使用相同的输入,哈希函数值每天都在变化

时间:2019-07-08 16:16:42

标签: ms-access-2016 sha512

我有一个简单的哈希函数,是从https://en.wikibooks.org/wiki/Visual_Basic_for_Applications/String_Hashing_in_VBA复制而来的。哈希值很好。我重新整理了文本,使其与保存的哈希值匹配。我在不同的日子再次尝试,相同的文本哈希值也不同。

Public Function SHA512(ByVal sIn As String, Optional bB64 As Boolean = 0) As String
    'Set a reference to mscorlib 4.0 64-bit

    'Test with empty string input:
    '128 Hex:   cf83e1357eefb8bd...etc
    '88 Base-64:   z4PhNX7vuL3xVChQ...etc

    Dim oT As Object, oSHA512 As Object
    Dim TextToHash() As Byte, bytes() As Byte

    Set oT = CreateObject("System.Text.UTF8Encoding")
    Set oSHA512 = CreateObject("System.Security.Cryptography.SHA512Managed")

    TextToHash = oT.Getbytes_4(sIn)
    bytes = oSHA512.ComputeHash_2((TextToHash))

    If bB64 = True Then
       SHA512 = ConvToBase64String(bytes)
    Else
       SHA512 = ConvToHexString(bytes)
    End If

    Set oT = Nothing
    Set oSHA512 = Nothing

End Function

Private Function ConvToHexString(vIn As Variant) As Variant

    Dim oD As Object

    Set oD = CreateObject("MSXML2.DOMDocument")

      With oD
        .LoadXML "<root />"
        .DocumentElement.DataType = "bin.Hex"
        .DocumentElement.nodeTypedValue = vIn
      End With
    ConvToHexString = Replace(oD.DocumentElement.Text, vbLf, "")

    Set oD = Nothing

End Function

1 个答案:

答案 0 :(得分:0)

解决了我的问题。哈希正在哈希一些随机缓存值。将值的传递重写为哈希函数。