How to get md5 of a file?

sdaly

New Member
Licensed User
Longtime User
Hi

I am trying to get the hash for a file. I know how to get the inputstream for the file and how to calculate the hash from the bytes. I just dont know ow to get the bytes from the inputstream/file?

Sub OpenFile(dir As String, filename As String)

Dim in As InputStream
in = File.OpenInput(dir,filename)
Dim buffer(1024) As Byte
'count = in.ReadBytes(buffer, 0, buffer.length)

Dim Bconv As ByteConverter
Dim data(0) As Byte
Dim md As MessageDigest

'data = Bconv.StringToBytes(msg, "UTF8")

data = md.GetMessageDigest(data, "MD5")
Msgbox(Bconv.HexFromBytes(data), "MD5 digest")

End Sub

Thanks
Scott
 

agraham

Expert
Licensed User
Longtime User
Get the file size from File.Size and make an byte array of that size.

InputStream.ReadBytes into that buffer checking for a return of -1 to make sure you've got the whole file which you most probably will have.

Pass the byte array directly to GetMessageDigest, I don't know why you've got a ByteConverter in there at all.
 
Upvote 0

sdaly

New Member
Licensed User
Longtime User
Thanks for the help...got it working with the below -

Dim in As InputStream
in = File.OpenInput(dir,filename)
Dim buffer(File.Size(dir, filename)) As Byte
count = in.ReadBytes(buffer, 0, buffer.length)
Dim Bconv As ByteConverter
Dim data(buffer.Length) As Byte
Dim md As MessageDigest
data = md.GetMessageDigest(buffer, "MD5")
Log("Hash: " & Bconv.HexFromBytes(data))
 
Upvote 0
Top