Created
July 30, 2015 12:23
-
-
Save miohtama/6e72c2458a7138599dc1 to your computer and use it in GitHub Desktop.
Generate full 128-bit randomness UUID version 4 on Python, break spefification to stir extra 6 bits of randomness
This file contains 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
def secure_uuid(): | |
"""Create a non-conforming 128-bit random version 4 UUID. | |
Random UUID is a RFC 4122 compliant UUID version 4 128-bit number. By default 6 fixed bits, 4 bits for version and 2 bits reserved for other purposes, are fixed. This function behaves like Python's ` uuid4()`` but also randomizes the remaining six bits, generating up to 128 bit randomness. | |
This function also sources all bytes from `os.urandom()` to guarantee the randomness and security and does not rely operating system libraries. | |
Using ``secure_uuid()`` poses a risk that generated UUIDs are not accepted when communicating with third party system. However, they are observed to be good for URLs and to be stored in PostgreSQL. | |
More information | |
* http://crypto.stackexchange.com/a/3525/25874 | |
* https://tools.ietf.org/html/rfc4122 | |
""" | |
return UUID(bytes=os.urandom(16), version=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment