Why can I not get hashing to work

cpopham

Member
Joined
Jun 3, 2004
Messages
20
Programming Experience
1-3
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.
VB.NET:
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 is "myFileMain" and "strOutFileArray3" ?
 
myFileMain is a Byte Array. The myFileMain stores the UTF-8 encoded original file.

The strOutFileArray3 is a string for the location of the file that has been written to disk with the hash and file length appended to the file.

Thanks, Chester
 
Okay, I went back to where I am orginally loading the code and converting it to a memorystream. And tested it by writing it back to disk. Now this bit of code worked fine yesterday, but, today it is adding a bunch of garbage to the end of the file.

Here is this code:
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] encoder [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] UTF8Encoding [/SIZE][SIZE=2][COLOR=#008000]'/Create UTF8 encoding (has more applications than ASCII)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fsIn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(strInFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) [/SIZE][SIZE=2][COLOR=#008000]'/Setup input filestream
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rdIn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamReader(fsIn) [/SIZE][SIZE=2][COLOR=#008000]'/Setup streamreader
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myfile [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE][SIZE=2]() [/SIZE][SIZE=2][COLOR=#008000]'/ByteArray to hold input file
[/COLOR][/SIZE][SIZE=2]myfile = encoder.GetBytes(rdIn.ReadToEnd) [/SIZE][SIZE=2][COLOR=#008000]'/Read input file in
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] msstream [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MemoryStream(myfile.Length) [/SIZE][SIZE=2][COLOR=#008000]'/Setup memorystream to hold data with length of data
[/COLOR][/SIZE][SIZE=2]msstream.Write(myfile, 0, myfile.Length) [/SIZE][SIZE=2][COLOR=#008000]'/Convert byte array of data to memorystream
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] after [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE][SIZE=2]() [/SIZE][SIZE=2][COLOR=#008000]'/Byte array to hold data from memorystream
[/COLOR][/SIZE][SIZE=2]after = msstream.ToArray [/SIZE][SIZE=2][COLOR=#008000]'/Convert memorystream to byte array
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'/Setup output Filestream
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fsOut [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(strOutFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] wr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamWriter(fsOut) [/SIZE][SIZE=2][COLOR=#008000]'/Setup streamwriter to output data
[/COLOR][/SIZE][SIZE=2]wr.Write(encoder.GetString(after)) [/SIZE][SIZE=2][COLOR=#008000]'/Convert byte array to string and write it to disk
[/COLOR][/SIZE][SIZE=2]wr.Flush() [/SIZE][SIZE=2][COLOR=#008000]'/Flush the streamwriter to make sure all data is written to disk[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]
[/COLOR][/SIZE]

 
I haven't read you last post yet, but taking your first post, change:
VB.NET:
myhash = SHA1.ComputeHash(myFi)
to:
VB.NET:
myhash = SHA1.ComputeHash(myFi.ToArray)
and the 'output' will display the same hash for both control-hash and from-file-hash
 
Back
Top