Skip to content

Instantly share code, notes, and snippets.

@scott-maddox
Created January 25, 2021 18:42
Show Gist options
  • Save scott-maddox/ef8887162ad9851e8ab069c29d62f327 to your computer and use it in GitHub Desktop.
Save scott-maddox/ef8887162ad9851e8ab069c29d62f327 to your computer and use it in GitHub Desktop.
Generate unguessable random unique IDs in python
# 24 bytes (192 bits) is more than sufficient to make any ID unguessable.
# If you're insanely paranoid, you can bump this to 48 bytes (384 bits).
#
# If you want to use a number of bytes that is not a multiple of 24,
# then you will need to strip the '=' characters from the end of the
# byte string generated by urlsafe_b64encode.
# See https://neilmadden.blog/2018/08/30/moving-away-from-uuids/ for how to
# estimate the amount of time it would require to guess an ID.
import os
from base64 import urlsafe_b64encode
print(urlsafe_b64encode(os.urandom(24)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment