0

I created a random string. First, I'd like to display the string and after that, I'd like to convert it to a MD5 hash. Can someone help me? I tried a lot but I can't get the right solution.

Here is my Code:

    public static string Generate(int lenght)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        return new string(Enumerable.Repeat(chars, lenght).Select(s => s[random.Next(s.Length)]).ToArray());

    }

    public static string MD5Hash()
    {
        StringBuilder hash = new StringBuilder();
        MD5CryptoServiceProvider md5provider = new MD5CryptoServiceProvider();
        byte[] bytes = md5provider.ComputeHash(new UTF8Encoding().GetBytes(Generate));

        for (int i = 0; i < bytes.Length; i++)
        {
            hash.Append(bytes[i].ToString("x2"));
        }

        return hash.ToString();
    }

Thank you in advance

Best Regards

Scott Arciszewski
  • 33,610
  • 16
  • 89
  • 206
Mike
  • 27
  • 9

1 Answers1

0

Either pass a string argument into the MD5Hash method (MD5Hash(string input) for example) OR you can use the Generate method inside the MD5Hash method (string input = Generate(*LengthYouWant*); for example).

Afterwards you can replace the Generate you've put inside of GetBytes currently with the input string.

Timmeh
  • 398
  • 2
  • 14