URL safe UUIDs in the smallest number of characters

后端 未结 2 1485
鱼传尺愫
鱼传尺愫 2021-02-13 00:15

Ideally I would want something like example.com/resources/äFg4вNгё5, minimum number of visible characters, never mind that they have to be percent

相关标签:
2条回答
  • 2021-02-13 00:41

    I use a url-safe base64 string. The following is some Python code that does this*.

    The last line removes '=' or '==' sign that base 64 encoding likes to put on the end, they make putting the characters into a URL more difficult and are only necessary for de-encoding the information, which does not need to be done here.

    import base64
    import uuid
    
    # get a UUID - URL safe, Base64
    def get_a_Uuid():
        r_uuid = base64.urlsafe_b64encode(uuid.uuid4().bytes)
        return r_uuid.replace('=', '')
    

    * This does follow the standards: base64.urlsafe_b64encode follows RFC 3548 and 4648 see https://docs.python.org/2/library/base64.html. Stripping == from base64 encoded data with known length is allowed see RFC 4648 §3.2. UUID/GUID are specified in RFC 4122; §4.1 Format states "The UUID format is 16 octets". The base64-fucntion encodes these 16 octets.

    0 讨论(0)
  • 2021-02-13 00:59

    Base-64 is good for this.

    {098ef7bc-a96c-43a9-927a-912fc7471ba2}
    

    could be encoded as

    vPeOCWypqUOSepEvx0cbog
    

    The usual equal-signs at the end could be dropped, as they always make the string-length a multiple of 4. And instead of + and /, you could use some safe characters. You can pick two from: - . _ ~

    More information:

    • RFC 4648
    • Storing UUID as base64 String (Java)
    • guid to base64, for URL (C#)
    • Short GUID (C#)
    0 讨论(0)
提交回复
热议问题