开发者

C# Generate a random Md5 Hash

开发者 https://www.devze.com 2023-03-02 02:51 出处:网络
How to generate a random Md5 开发者_运维问答hash value in C#?A random MD5 hash value is effectively just a 128-bit crypto-strength random number.

How to generate a random Md5 开发者_运维问答hash value in C#?


A random MD5 hash value is effectively just a 128-bit crypto-strength random number.

var bytes = new byte[16];
using (var rng = new RNGCryptoServiceProvider())
{
    rng.GetBytes(bytes);
}

// and if you need it as a string...
string hash1 = BitConverter.ToString(bytes);

// or maybe...
string hash2 = BitConverter.ToString(bytes).Replace("-", "").ToLower();


You could create a random string using Guid.NewGuid() and generate its MD5 checksum.


using System.Text;
using System.Security.Cryptography;

  public static string ConvertStringtoMD5(string strword)
{
    MD5 md5 = MD5.Create();
    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(strword);
    byte[] hash = md5.ComputeHash(inputBytes);
    StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hash.Length; i++)
       { 
            sb.Append(hash[i].ToString("x2"));
       }
       return sb.ToString();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消