I need to generate a uuid in python.
The application I want to interface with uses 22-characters uuids. These seem to be generated by generating a char-32 uuid and base64-encoding it.
Now, if I try to do that in python, my (base64 encoded) uuid always has 24 characters.
For the moment I just "cut off" the encoded uuid after 22 chars. Not sure if this is valid though. Chances are I'll get duplicates this way. Am I maybe making a mistake in generating my uuid?
My code:
import uuid import base64 my_uuid = new_occ_id = base64.urlsafe_b64encode(uuid.uuid4().bytes) my_uuid_22 = new_occ_id = my_uuid[0:22] print(my_uuid) print(my_uuid_22) ...yields (!!Corrected output!!):
b'1K-HAjUjEemzDxwbtRxjwA==' b'1K-HAjUjEemzDxwbtRxjwA' Sample UUID from the application:
051MZjd97kcBgtZiEH}IvW
}is not valid in base 64, so the program must use something different than base 64.shortuuidwhich uses base57 instead of base64. Base57 natively produces 11 characters for each 64 bytes, without padding, and is more url friendly.