简体   繁体   中英

Random generated string to md5 hash

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM