Why can I not get hashing to work

cpopham

Well-known member
Joined
Feb 18, 2004
Messages
273
I input a XML file with UTF-8 encoding. I get the file length and hash the file, I write out the file and append the hash and length to the file.

I input this newly created file, extract the hash and length from it and then calculate the hash on data. When I view the data before and after hashing, they look the same, However, the hashes are coming up completely different.

Could someone look at my code and see where I may be makiong my mistake.

Code:
Private Sub CreateAppHash()

        Dim encoder As New UTF8Encoding To encode the data

        Dim SHA1 As New SHA1CryptoServiceProvider For hashing
        Dim bytHash As Byte() To store the hash
        bytHash = SHA1.ComputeHash(myFileMain) compute the hash (bytHash is a byte array)
        Dim strHash As String = Convert.ToBase64String(bytHash) convert the hash to base 64
        Dim bytHashes As Byte() To hold UTF-8 encoded hash
        bytHashes = encoder.GetBytes(strHash) Convert hash to UTF-8
        Dim myStream As New MemoryStream(myFileMain.Length) Create a memorystream to hold the data      
        myStream.Write(myFileMain, 0, myFileMain.Length) Copy the data to the memorystream (myfileMain is module 
         level and has been filled with an XML UTF-8 encoded file
        Dim length As Integer = myFileMain.Length Get length of memorystream
        Dim strLen As String = CInt(length) convert length to string   
        strLen = strLen.PadLeft(30, "0") Make sure the length is 30 chracters long
        Dim bytLen As Byte() = encoder.GetBytes(strLen) convert the length to UTF-8 bytes ans store in byte array


        Dim fsOut As New FileStream(strOutFileArray3, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
        Dim wr As New StreamWriter(fsOut) setup streamwriter to output data
        wr.Write(encoder.GetString(myFileMain)) Convert byte array to string and write it to disk
        wr.Write(encoder.GetString(bytHashes))
        wr.Write(encoder.GetString(bytLen))


        wr.Flush() Flush the streamwriter to make sure all data is written to disk
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////
        CHECK THE HASH
        Get the file with the Hash appended to it and put it in a byte array
        Dim fsHashed As New FileStream(strOutFileArray3, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
        Dim rdrHash As New StreamReader(fsHashed)
        Dim bytHashed As Byte()
        bytHashed = encoder.GetBytes(rdrHash.ReadToEnd)
        Dim msLength As New MemoryStream(30) Set up memorystream to get the length of the original file
        msLength.Write(bytHashed, bytHashed.Length - 30, 30)
        Dim bytLength As Byte()
        Dim intLe As Integer = CInt(encoder.GetString(msLength.ToArray)) Convert original file length to integer
        Dim myFi As New MemoryStream(intLe)
        myFi.Write(bytHashed, 0, intLe) Get the file data minus file length with padding and hash code

        Output the extracted data to see how it looks
        Dim fsOup As New FileStream("tester.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
        Dim rdrOP As New StreamWriter(fsOup)
        rdrOP.Write(fsOup)
        rdrOP.Flush()
        Dim myhash As Byte() Setup to calculate hash on the extracted data
        Dim strHasher As String
        myhash = SHA1.ComputeHash(myFi) compute Hash on extracted data
        strHasher = Convert.ToBase64String(myhash) Convert it to base 64

        Dim bytHashs As Byte() = encoder.GetBytes(strHasher) encode hash to byte using UTF-8
        Dim bytOld As Byte() To hash extracted from file data
        Dim msO As New MemoryStream(100)
        msO.Write(bytHashed, intLe, bytHashed.Length - 30 - intLe) Get hash from uploaded file
        bytOld = msO.ToArray convert extracted hash to byte array
        Dim strOlP, strNwP As String to compare extracted hash with newly calculated hash
        strOlP = encoder.GetString(bytOld)
        strNwP = encoder.GetString(bytHashs)
        MessageBox.Show(strOlP & vbCrLf & vbCrLf & strNwP) Compare hashes

This will be a part of a bigger project that will be using memory streams. That is why I use a memorystream quite a bit in the code.

Thanks,
Chester
 
What version of .Net are you using as the line
Code:
 myStream.Write(myFileMain, 0, myFileMain.Length)
will not compile under VS 2003 as the first parameter should be a byte aray not a stream.

Also IIRC you do not need to allocate a size when you declare the memorystream
Code:
im myStream As New MemoryStream(myFileMain.Length)
plus I think you are allocating one byte too many here as the memory stream is zero based but the strings Length property is one based.

Also as you read through the code you seem to be doing a lot of string -> byte conversions and then use encoder.GetString() on the bytes to do a byte->string conversion, it is probably easier to either use the byte arrays or use strings rather than converting back and fore between them.
 
Thanks, PlausiblyDamp. Well, I am trying to figure out hashing and then encryption.

The project I am working on will start another application, then redirect its output into a memorystream. I want to hash the data in the memorystream and then encrypt the data before outputting it to disk. I just wanted to be sure that I could get my code working reliably just with hashing and then with cryptography before I use it in the project.

Thanks, Chester
 
Back
Top