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

前端 未结 13 1331
被撕碎了的回忆
被撕碎了的回忆 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 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!

提交回复
热议问题