What's the best way to create a short hash, similar to what tiny Url does?

前端 未结 13 1291
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 09:10

I\'m currently using MD5 hashes but I would like to find something that will create a shorter hash that uses just [a-z][A-Z][0-9]. It only needs to be around 5-

相关标签:
13条回答
  • 2020-12-04 09:56

    First I get a list of random distinct numbers. Then I select each char from base string, append and return result. I'm selecting 5 chars, that will amount to 6471002 permutations out of base 62. Second part is to check against db to see if any exists, if not save short url.

     const string BaseUrlChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    
     private static string ShortUrl
     {
         get
         {
             const int numberOfCharsToSelect = 5;
             int maxNumber = BaseUrlChars.Length;
    
             var rnd = new Random();
             var numList = new List<int>();
    
             for (int i = 0; i < numberOfCharsToSelect; i++)
                 numList.Add(rnd.Next(maxNumber));
    
             return numList.Aggregate(string.Empty, (current, num) => current + BaseUrlChars.Substring(num, 1));
          } 
      }
    
    0 讨论(0)
  • 2020-12-04 09:58

    You could take the first alphanumeric 5-10 characters of the MD5 hash.

    0 讨论(0)
  • 2020-12-04 10:00

    There's a wonderful but ancient program called btoa which converts binary to ASCII using upper- and lower-case letters, digits, and two additional characters. There's also the MIME base64 encoding; most Linux systems probably have a program called base64 or base64encode. Either one would give you a short, readable string from a 32-bit CRC.

    0 讨论(0)
  • 2020-12-04 10:02

    Just take a Base36 (case-insensitive) or Base64 of the ID of the entry.

    So, lets say I wanted to use Base36:

    (ID - Base36)
    1 - 1
    2 - 2
    3 - 3
    10 - A
    11 - B
    12 - C
    ...
    10000 - 7PS
    22000 - GZ4
    34000 - Q8C
    ...
    1000000 - LFLS
    2345000 - 1E9EW
    6000000 - 3KLMO

    You could keep these even shorter if you went with base64 but then the URL's would be case-sensitive. You can see you still get your nice, neat alphanumeric key and with a guarantee that there will be no collisions!

    0 讨论(0)
  • 2020-12-04 10:05

    You cannot use a short hash as you need a one-to-one mapping from the short version to the actual value. For a short hash the chance for a collision would be far too high. Normal, long hashes, would not be very user-friendly (and even though the chance for a collision would probably be small enough then, it still wouldn't feel "right" to me).

    TinyURL.com seems to use an incremented number that is converted to Base 36 (0-9, A-Z).

    0 讨论(0)
  • 2020-12-04 10:06

    You could encode your md5 hash code with base64 instead of hexadecimal, this way you get a shorter url using exactly the characters [a-z][A-Z][0-9].

    0 讨论(0)
提交回复
热议问题