将数据表转换为字节数组并比较散列

时间:2015-10-01 21:24:13

标签: vb.net bytearray

我试图将数据表转换为字节数组,然后创建一个哈希值并进行比较。基本上我将通过wcf服务转移dt并在另一端比较它,但现在我想测试。由于某种原因,当我在同一个数据表上再次转换为字节数组时,哈希是不同的...想知道为什么?任何想法谢谢

Private Sub Button1_Click(发件人为对象,e为EventArgs)处理Button1.Click

    Dim dtTest As New System.Data.DataTable("test")
    dtTest.Columns.Add("Account", GetType(System.String))
    dtTest.Columns.Add("Description", GetType(System.String))

    dtTest.Rows.Add("test1", "test2")

    Dim stream As New System.IO.MemoryStream()
    Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
    formatter.Serialize(stream, dtTest)

    Dim bytes() As Byte = stream.GetBuffer()
    Dim fileHash As Byte() = ComputeFileHash(bytes)

    'Dim array() As Byte = System.Text.Encoding.ASCII.GetBytes()

    Dim stream2 As New System.IO.MemoryStream()
    Dim formatter2 As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
    formatter.Serialize(stream, dtTest)

    Dim bytes2() As Byte = stream.GetBuffer()
    Dim NewFileHash As Byte() = ComputeFileHash(bytes2)



    If CompareByteHashes(bytes2, bytes) Then

        MessageBox.Show("They are the same, no changes were made.")

    Else

        MessageBox.Show("The file was changed.")


    End If

    '        System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes); 
    'System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

    'DataTable dtUsers = (DataTable)formatter.Deserialize(stream)

End Sub


Private Function ComputeFileHash(ByVal dt As Byte()) As Byte()

    Dim ourHash(0) As Byte



    ' If file exists, create a HashAlgorithm instance based off of MD5 encryption

    ' You could use a variant of SHA or RIPEMD160 if you like with larger hash bit sizes.



    Try

        Dim ourHashAlg As HashAlgorithm = HashAlgorithm.Create("MD5")

        ' Dim sha As New SHA1CryptoServiceProvider()
        ' Dim result As Byte() = sha.ComputeHash(dt)



        'Compute the hash to return using the Stream we created.

        ourHash = ourHashAlg.ComputeHash(dt)


    Catch ex As IOException

        MessageBox.Show("There was an error opening the file: " & ex.Message)

    End Try





    Return ourHash
End Function





Private Function CompareByteHashes(ByVal newHash As Byte(), ByVal oldHash As Byte()) As Boolean

    ' If any of these conditions are true, the hashes are definitely not the same.

    If newHash Is Nothing Or oldHash Is Nothing Or newHash.Length <> oldHash.Length Then

        Return False

    End If

    ' Compare each byte of the two hashes. Any time they are not the same, we know there was a change.

    For i As Integer = 0 To newHash.Length - 1

        If newHash(i) <> oldHash(i) Then

            Return False

        End If

    Next i



    Return True

End Function

1 个答案:

答案 0 :(得分:0)

看起来像一个错字;您正在重用已经读过的流。改变如下:

    formatter.Serialize(stream2, dtTest)

    Dim bytes2() As Byte = stream2.GetBuffer()