File MD5# and SHA# Calculator
After seeing a long-winded version on the forums about how to generate a MD5# for a file I thought I'd post how to do this here...
vb.net Code:
Private Function CalcMD5(ByVal File As String) As String
Dim f = New IO.FileStream(File, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read, 4096)
Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
Return Join((From xItem In md5.ComputeHash(f) Select xItem.ToString("X2")).ToArray, "")
End Function
Also you could use the following to calculate the older (and slower) SHA#:
vb.net Code:
Private Function CalcSHA(ByVal File As String) As String
Dim f = New IO.FileStream(File, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read, 4096)
Dim sha1 As New System.Security.Cryptography.SHA1CryptoServiceProvider
Return Join((From xItem In sha1.ComputeHash(f) Select xItem.ToString("X2")).ToArray, "")
End Function
Hope this helps some people ...
Also please provide feedback and say thanks if this helped you
Kris
Re: File MD5# and SHA# Calculator
You've got your facts a bit backwards there... SHA-X is much newer than MD5 and MD5 is now deemed somewhat insecure. Several collisions have been found for MD5 but none for SHA at all (except for reduced SHA). Also, a test has shown that .NET MD5 and SHA-1 implementations are about the same speed.
Re: File MD5# and SHA# Calculator
Hrm interesting ... i know that SHA is more accurate... but MD5 should be quite accurate also as the chances of duplication are 1 in 16^32 arn't they, also i remember old zip programs using sha and now they all seem to use md5?
Kris
Re: File MD5# and SHA# Calculator
I've never used hashing ZIP programs so I wouldn't know. Maybe they've always supported SHA but had nothing to do so decided to add other options? :)