Quantcast 2 ways to hash a string to md5 type

2 ways to hash a string to md5 type

by Neon Quach 4. August 2011 20:49

In this blog I intro to all of you 2 ways to hash a string to md5 type, using .net C#.

The first 1st:

Instantiate the MD5CryptoServiceProvider.
Convert your string to a byte array.
Run that byte array through the crypto provider to compute the hash (and get a byte array back)
And instead of just converting that byte array back to a string, you need to run through each byte and convert it via this:

Here is my method:

    ///<summary>

    /// Encrypts the string using md5

    ///</summary>

    ///<param name="text">The text.</param>

    ///<returns>Md5 encrupted string</returns>

    internal static string Md5(string text)

    {

        MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();

        byte[] bs = Encoding.UTF8.GetBytes(text);

        bs = x.ComputeHash(bs);

        StringBuilder sb = new StringBuilder();

        foreach (byte b in bs)

        {

            sb.Append(b.ToString("x2").ToUpper());

        }

        return sb.ToString();

    }

Second 2nd:

Call the HashPasswordForStoringInConfigFile method in FormsAuthentication class and pass a string which you want to hash, and hash algorithm (in this case is md5 by default).

Here is my statment:
FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");


Note: You must add System.Web.dll and using System.Web.Security namespace to call method

My example code:

using System;

using System.Text;

using System.Web.Security;

using System.Security.Cryptography;

 

namespace HasingUtil

{

    internal class Program

    {

        private static void Main()

        {

            const string password = "Secret pa$$word";

            string md51 = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");

            string md52 = Md5(password);

            Console.WriteLine(md51);

            Console.WriteLine(md52);

            Console.Read();

        }

 

        ///<summary>

        /// Encrypts the string using md5

        ///</summary>

        ///<param name="text">The text.</param>

        ///<returns>Md5 encrupted string</returns>

        internal static string Md5(string text)

        {

            MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();

            byte[] bs = Encoding.UTF8.GetBytes(text);

            bs = x.ComputeHash(bs);

            StringBuilder sb = new StringBuilder();

            foreach (byte b in bs)

            {

                sb.Append(b.ToString("x2").ToUpper());

            }

            return sb.ToString();

        }

    }

}


Happy hashing!

 

Tags:


Categories: c#

blog comments powered by Disqus

About me

I'm  currently employed as Software developer at devinition.com and also a Microsoft Certified Technology Specialist (MCTS), Microsoft Certified Professional Developer (MCPD) in Net Framework 2.0 and 3.5: Web Applications and MCTS .NET Framework 3.5, ADO.NET Applications

Powered by BlogEngine.NET 2.5.0.5 - Eco Theme by n3o Web Designers