-
-
Save fengjing/d634c0f9fd02c34f00433e4f45b7f740 to your computer and use it in GitHub Desktop.
Get a secret random string in python(Refer to django.utils.crypto.get_random_string)
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
| import uuid | |
| def get_guid(): | |
| """Get a globally unique string for identify things""" | |
| return uuid.uuid4().hex |
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
| import random | |
| import hashlib | |
| import time | |
| def get_random_string(length=12, | |
| allowed_chars='abcdefghijklmnopqrstuvwxyz' | |
| 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'): | |
| """ | |
| See also: | |
| https://github.com/rs/xid (globally unique id generator) | |
| https://stackoverflow.com/questions/41354205/how-to-generate-a-unique-auth-token-in-python | |
| https://docs.python.org/3/whatsnew/3.6.html#secrets | |
| """ | |
| random.seed( | |
| hashlib.sha256( | |
| ("%s%s%s" % ( | |
| random.getstate(), | |
| time.time(), | |
| 'O_O-SECRET_KEY')).encode('utf-8') | |
| ).digest()) | |
| return ''.join(random.choice(allowed_chars) for i in range(length)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment