Avis Junior Poster

Joined: 07 Oct 2003 Posts: 510 Location: India
|
Posted: Aug 27th, 2004 02:07 AM Post subject: Create a Cryptographic MD5 hash in VB.NET |
|
|
An MD5 hash (also called a message digest) is an excellent way to perform one-way data authentication. For example, you can use before-and-after MD5 hashes to determine whether the contents of a file have changed. Another common use for hashes is for password authentication: instead of dealing with the password directly, in clear text, you can hash the password, then compare that hash to a stored hash. Using this technique, you never jeopardize the security of the password, because the hash only works in one direction. In fact, this is basically how Windows authenticates passwords.
In the days of VB6, creating a hash was possible using the Win32 CryptoAPI, but it wasn't a walk in the park by any stretch. But if you're using VB.NET, it's a snap to use classes from the System.Security.Cryptography namespace to do the job. Here's a generic function that returns an MD5 hash, formatted as a String, from the contents of a String you pass into the function.
[vb:1:4e5f5c708d]Imports System.Text
Imports System.Security.Cryptography
Private Function GenerateHash(ByVal SourceText As String) As String
'Create an encoding object to ensure the encoding standard for the source text
Dim Ue As New UnicodeEncoding()
'Retrieve a byte array based on the source text
Dim ByteSourceText() As Byte = Ue.GetBytes(SourceTStext)
'Instantiate an MD5 Provider object
Dim Md5 As New MD5CryptoServiceProvider()
'Compute the hash value from the source
Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
'And convert it to String format for return
Return Convert.ToBase64String(ByteHash)
End Function[/vb:1:4e5f5c708d] _________________ Code Snippets, Tutorials, Utilities, Controls
Low cost Web Hosting
Hosting starts at as low as $4 per year!
Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags! |
|