How to generate a unique hash for a URL?

前端 未结 12 1384
时光取名叫无心
时光取名叫无心 2020-12-28 08:11

Given these two images from twitter.

http://a3.twimg.com/profile_images/130500759/lowres_profilepic.jpg
http://a1.twimg.com/profile_images/58079916/lowres_pr         


        
12条回答
  •  感情败类
    2020-12-28 09:01

    You said:

    I don't want a cryptographic algorithm as it this needs to be a performant operation.

    Well, I understand your need for speed, but I think you need to consider drawbacks from your approach. If you just need to create hash for urls, you should stick with it and don't to write a new algorithm, where you'll need to deal with collisions, for instance.

    So you could have a Dictionary to work as a cache to your urls. So, when you get a new address, you first do a lookup in that list and, if doesn't find a match, hash it and storage for future usage.

    Following this line, you could give MD5 a try:

    public static void Main(string[] args)
    {
        foreach (string url in new string[]{ 
            "http://a3.twimg.com/profile_images/130500759/lowres_profilepic.jpg", 
            "http://a1.twimg.com/profile_images/58079916/lowres_profilepic.jpg" })
        {
            Console.WriteLine(HashIt(url));
        }
    }
    
    private static string HashIt(string url)
    {
        Uri path = new Uri(new Uri(url), ".");
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte[] data = md5.ComputeHash(
            Encoding.ASCII.GetBytes(path.OriginalString));
        return Convert.ToBase64String(data);
    }
    

    You'll get:

    rEoztCAXVyy0AP/6H7w3TQ==
    0idVyXLs6sCP/XLBXwtCXA==
    

提交回复
热议问题