Skip to content

Instantly share code, notes, and snippets.

@seanpianka
Last active August 18, 2023 21:07
Show Gist options
  • Save seanpianka/11ba7d6b2f2683ca8768c72856c07565 to your computer and use it in GitHub Desktop.
Save seanpianka/11ba7d6b2f2683ca8768c72856c07565 to your computer and use it in GitHub Desktop.
Generate a random hex string/key in Python 3
def generate_hex_string(length: int):
""" Generate a randomized hex string.
Parameters
----------
length : int
Desired character length of the returned token.
Returns
-------
str
Randomly generated token with length of `length`.
"""
return "%030x" % random.randrange(16 ** length)
@CroneKorkN
Copy link

It generates hex, not b64.

@seanpianka
Copy link
Author

Thanks for catching that 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment