user6704961
user6704961

Reputation:

Shorter version of UUID4

I'm generating in python uuid4 strings.

I use it to identify my service accounts, but one of my systems (GCP service accounts) has a 30 char limit, and it's too late to use something else :

Service account ID must be between 6 and 30 characters.
Service account ID must start with a lower case letter, followed by one or more lower case alphanumerical characters that can be separated by hyphens.

How can I have a shorter version of the UUID with a limited conflict risk ?

I've seen some base64 encoding hacks, but the shortest I can do is 22. Ideally, I would have something like git commit hash since the risk of conflict is limited.

Upvotes: 2

Views: 4481

Answers (2)

wim
wim

Reputation: 362458

How can I have a shorter version of the UUID with a limited conflict risk?

Drop the character at index 12 since that is always a 4 - not very random.

Similarly drop the character at index 16 since it's always in 8 9 a b.

You'll be left with 30 fairly random characters in [0-9a-f]. Collisions are still small enough to ignore. Note that uuid4s may start with numbers, so you may need to use rejection sampling in your generator (or just cut out the middleman and generate your ids directly with random, realistically there is not much point to use uuid4 at all here).

Upvotes: 4

savvamadar
savvamadar

Reputation: 294

Use the first 15 characters and the last 15 characters of the UUID. Omit everything in between.

import uuid

uuid_str = str(uuid.uuid4()).replace("-","")
uuid_str = uuid_str[:15]+uuid_str[-15:]

Upvotes: 0

Related Questions