URL safe UUIDs in the smallest number of characters

后端 未结 2 888
难免孤独
难免孤独 2021-02-13 00:28

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条回答
  •  Happy的楠姐
    2021-02-13 00:43

    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.

提交回复
热议问题