13 Jul

C# Create A MD5 Hash From A String

This example shows how to create a MD5 hash from a string.


            string hash = CreateMD5Hash("http://www.csharpexamples.com");
        public string CreateMD5Hash(string input)
            // Step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);
            // Step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            return sb.ToString();

Leave a Reply

Your email address will not be published. Required fields are marked *