简体   繁体   中英

How to create persistent unique MD5 hash

I suppose if i use default MD5 function, that would create same hash for the same string accross different applications

So somehow i want to define a pre static value, give it to MD5 function, and make those generated results to unique my application.

This static value will never change accross my application so it will always generate same hash value for same string accross my application

But if another application uses default MD5, they won't get same MD5 hash value

How can i do this with c# 5 ? asp.net

Here below function i have

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();
}

Create a GUID unique for your application, prepend that to the data you want hashing (before you hash it) then perform your hash.

It'll still be an MD5 hash and it will be unique to your application.

If you need to verify the hash in the future though, be sure to add the same GUID as before.

    private const string _myGUID = "{C05ACA39-C810-4DD1-B138-41603713DD8A}";
    public static string ConvertStringtoMD5(string strword)
    {
        MD5 md5 = MD5.Create();
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(_myGUID + 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();
    }

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