Compute the hash of a file.

xconspirisist

New member
Joined
Jun 4, 2006
Messages
1
Programming Experience
10+
I would like to be able to compute the hash of a file. (MD5/SHA1).

Am I correct that the most appropriate approach would be to manually read the file into an input stream, and then pass that to a "System.Security.Cryptography.HashAlgorithm"?

If anyone could post and sample/psudo code, that would be massively appreciated.
 
"How to compute and compare hash values by using Visual Basic .NET or Visual Basic 2005" http://support.microsoft.com/default.aspx?scid=kb;en-us;q301053

A code example, reading a file and computing a md5 hash, you may compare by bytes or string later
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] filename [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Application.StartupPath & "\mycalc.exe"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fs [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] buffer(fs.Length - 1) [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Byte
[/COLOR][/SIZE][SIZE=2]fs.Read(buffer, 0, fs.Length)
fs.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] tmpHash() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MD5CryptoServiceProvider().ComputeHash(buffer)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strHash [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Convert.ToBase64String(tmpHash)[/SIZE]

 
Back
Top