gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
md5 hash in c#
- User
md5 hash in c#
- Assistant
You can use the
System.Security.Cryptographynamespace to generate an MD5 hash in C#. Here's an example of how to create a function that generates an MD5 hash for a given input string:using System; using System.Text; using System.Security.Cryptography; public class MD5Hash { public static void Main(string[] args) { string inputString = "This is a test string"; string md5Hash = GenerateMD5Hash(inputString); Console.WriteLine("Input String: " + inputString); Console.WriteLine("MD5 Hash: " + md5Hash); } public static string GenerateMD5Hash(string input) { MD5 md5 = MD5.Create(); // Convert the input string to a byte array byte[] inputBytes = Encoding.ASCII.GetBytes(input); // Compute the hash byte[] hashBytes = md5.ComputeHash(inputBytes); // Convert the byte array to a hexadecimal string StringBuilder result = new StringBuilder(hashBytes.Length * 2); for (int i = 0; i < hashBytes.Length; i++) { result.Append(hashBytes[i].ToString("x2")); } return result.ToString(); } }