计算文件HASH返回不同的值

发布于 2024-07-16 13:38:20 字数 1300 浏览 6 评论 0原文

有谁知道,为什么下面的代码在某些机器上返回不同的结果?

Private Shared Function ComputeHashValue(ByVal Data As String) As String
    Dim HashAlgorithm As SHA512 = SHA512.Create
    Dim HashValue() As Byte = HashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(Data))
    ' Looping over the array and ANDing each byte with 0111111
    For i As Integer = 0 To HashValue.Length - 1
        HashValue(i) = HashValue(i) And Convert.ToByte(127)
    Next
    Return Encoding.ASCII.GetString(HashValue)
End Function

Private Shared Function AreByteArraysEqual(ByVal array1 As Byte(), ByVal array2 As Byte()) As Boolean
    If array1.Length <> array2.Length Then Return False
    For i As Integer = 0 To array1.Length - 1
        If array1(i) <> array2(i) Then Return False
    Next
    Return True
End Function

Private Shared Sub SomeMethod()
    Dim t_prvBytes() As Byte = New Byte() {SOME VALUES} 'Previously computed HASH
    Dim t_dllStream As New IO.FileStream("C:\myfile.txt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)

    Dim t_reader As New IO.StreamReader(t_dllStream)

    Dim t_dllHash() As Byte = System.Text.Encoding.Unicode.GetBytes(ComputeHashValue(t_reader.ReadToEnd))

    MsgBox(AreByteArraysEqual(t_dllHash, t_prvBytes))

    t_dllStream.Close()
End Function

Does anybody know, why the following code returns different results on some machines?

Private Shared Function ComputeHashValue(ByVal Data As String) As String
    Dim HashAlgorithm As SHA512 = SHA512.Create
    Dim HashValue() As Byte = HashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(Data))
    ' Looping over the array and ANDing each byte with 0111111
    For i As Integer = 0 To HashValue.Length - 1
        HashValue(i) = HashValue(i) And Convert.ToByte(127)
    Next
    Return Encoding.ASCII.GetString(HashValue)
End Function

Private Shared Function AreByteArraysEqual(ByVal array1 As Byte(), ByVal array2 As Byte()) As Boolean
    If array1.Length <> array2.Length Then Return False
    For i As Integer = 0 To array1.Length - 1
        If array1(i) <> array2(i) Then Return False
    Next
    Return True
End Function

Private Shared Sub SomeMethod()
    Dim t_prvBytes() As Byte = New Byte() {SOME VALUES} 'Previously computed HASH
    Dim t_dllStream As New IO.FileStream("C:\myfile.txt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)

    Dim t_reader As New IO.StreamReader(t_dllStream)

    Dim t_dllHash() As Byte = System.Text.Encoding.Unicode.GetBytes(ComputeHashValue(t_reader.ReadToEnd))

    MsgBox(AreByteArraysEqual(t_dllHash, t_prvBytes))

    t_dllStream.Close()
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯角 2024-07-23 13:38:20

您不应该通过 Encoding.ASCII 将哈希值转换为文本。 它不是 ASCII 文本。 (它根本不是文本。)您还对使用 Encoding.Unicode 读取的原始文本进行 ASCII 编码的结果进行哈希处理。 为什么?

您正在文本和二进制形式之间进行各种转换 - 并且您可能不应该执行任何操作。 只需对二进制数据进行哈希处理(使用 HashAlgorithm.ComputeHash(Stream)),并将结果也保留为二进制。 如果您确实需要将二进制数据转换为文本,请使用Convert.ToBase64String

此外,您将数据与先前计算的值进行比较 - 但您尚未解释先前计算的值从何而来。

You shouldn't be converting the hash into text via Encoding.ASCII. It's not ASCII text. (It's not text at all.) You're also hashing the result of ASCII-encoding the original text, which you read in using Encoding.Unicode. Why?

You're doing all kinds of conversions between text and binary forms - and you probably shouldn't be doing any. Just hash the binary data (using HashAlgorithm.ComputeHash(Stream)), and keep the result in binary too. If you really need to convert the binary data into text, use Convert.ToBase64String.

In addition, you're comparing the data with a previously computed value - but you haven't explained where that previously computed value came from to start with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文