An example of string to MD5 code function realized by C

  • 2021-10-24 23:26:39
  • OfStack

In this paper, an example is given to describe the function of transforming string into MD5 code realized by C #. Share it for your reference, as follows:


/*
 Test environment :WinXP SP3 , Visual Studio 2008 SP1 , Visual Studio 2010 SP1
 Last update date: 2014-04-23
*/
public string CalculateMD5Hash(string input)
{
  MD5 md5 = System.Security.Cryptography.MD5.Create();
  byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
  byte[] hash = md5.ComputeHash(inputBytes);
  // step 2, convert byte array to hex string
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < hash.Length; i++)
  {
  sb.Append(hash[i].ToString("X2"));
  }
  return sb.ToString();
}//end func

More readers interested in C # can check the topic of this site: "C # String Operation Skills Summary", "C # Programming Thread Use Skills Summary", "C # Operating Excel Skills Summary", "XML File Operation Skills Summary in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: