Learning never exhausts the mind
Home >  Coding > C# ASP.Net MVC > Generate MD5 Hash of String

Published 25th June 2009 by

This short snippet will generate an MD5 hash of a given string value in C#

Simply pass in a string and the function will return a MD5 hash of the contents.

public string CalculateMD5Hash(string input) 
  // Calculate MD5 hash from input 
  var md5 = MD5.Create(); 
  byte[] inputBytes = Encoding.ASCII.GetBytes(input); 
  byte[] hash = md5.ComputeHash(inputBytes); 

  // Convert byte array to hex string 
  var sb = new StringBuilder(); 
  for (int i = 0; i < hash.Length; i++) 

  return sb.ToString(); 

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.