Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » .NET Knowledge Base

Post new topic   Reply to topic
Create a Cryptographic MD5 hash in VB.NET
View previous topic :: View next topic  
Author Message
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Aug 27th, 2004 02:07 AM    Post subject: Create a Cryptographic MD5 hash in VB.NET Reply with quote

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!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » .NET Knowledge Base All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic