Skip to content

Instantly share code, notes, and snippets.

@miohtama
Created July 30, 2015 12:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miohtama/6e72c2458a7138599dc1 to your computer and use it in GitHub Desktop.
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
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