Skip to content

Instantly share code, notes, and snippets.

@hfukada
Created August 25, 2016 18:55
Show Gist options
  • Save hfukada/bbdee14e6bb098d73b6ce732bb2a2379 to your computer and use it in GitHub Desktop.
Save hfukada/bbdee14e6bb098d73b6ce732bb2a2379 to your computer and use it in GitHub Desktop.
def gen_nonce():
"""Generates a random string of bytes, base64 encoded"""
length = 64
string = base64.b64encode(os.urandom(length), altchars=b'-_')
b64len = 4 * min(length, 3)
if length % 3 == 1:
b64len += 2
elif length % 3 == 2:
b64len += 3
return string[0:b64len].decode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment