Your Ad Here

ASP.NET md5 function

Date: 29 Jan 2010 Comments: 1
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in D:\Hosting\3681091\html\blog\wp-content\plugins\digg-digg\dd.class.php on line 759

Warning: file_get_contents(http://feeds.delicious.com/v2/json/urlinfo/data?url=http%3A%2F%2Fwww.devtheweb.net%2Fblog%2F2010%2F01%2F29%2Fasp-net-md5-function%2F) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in D:\Hosting\3681091\html\blog\wp-content\plugins\digg-digg\dd.class.php on line 759

I was pleasantly surprised to found that using System.Security.Cryptography you can create a function that calculates the MD5 hash of string.

Here’s an C# example:

using System.Security.Cryptography;
using System.Text;

public string md5(string aString)
{
byte[] byteStr = Encoding.UTF8.GetBytes(aString);
MD5 md5Provider = new MD5CryptoServiceProvider();

return Encoding.UTF8.GetString(md5Provider.ComputeHash(byteStr));
}

Here’s the same function in VB.NET:

Imports System.Security.Cryptography
Imports System.Text

Public Function md5(ByVal aString As String) As String
Dim byteStr() As Byte =  Encoding.UTF8.GetBytes(aString)
Dim md5Provider As MD5 =  New MD5CryptoServiceProvider()

Return Encoding.UTF8.GetString(md5Provider.ComputeHash(byteStr))
End Function

Does System.Security.Cryptography namespace provides all necessary cryptographic services for you?
  1. One Comments to “ASP.NET md5 function”

    1. Richard says:

      Yeah they have all kinds of amazing functions. You can even make strings into UPPERCASE! Absolutely amazing, I have no idea how they do it.

    Leave a Reply


    Spam protection by WP Captcha-Free