SoFunction
Updated on 2025-03-08

C# implements a method to generate MD5 hash for a given string

This article example describes the method of C# to generate MD5 hash for a given string. Share it for your reference. The specific analysis is as follows:

Here we first need a reference to the following namespace:

Copy the codeThe code is as follows:
;
;

The main code is as follows:

/// <summary>
/// method to generate a MD5 hash of a string
/// </summary>
/// <param name="strToHash">string to hash</param>
/// <returns>hashed string</returns>
public string GenerateMD5(string str)
{
  MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  byte[] byteArray = (str);
  byteArray = (byteArray);
  string hashedValue = "";
  foreach (byte b in byteArray)
  {
    hashedValue += ("x2");
  }
  return hashedValue;
}

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