Created
January 25, 2021 18:42
-
-
Save scott-maddox/ef8887162ad9851e8ab069c29d62f327 to your computer and use it in GitHub Desktop.
Generate unguessable random unique IDs in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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