(length=64, encoding="hex")
| 27 | |
| 28 | |
| 29 | def random(length=64, encoding="hex"): |
| 30 | if encoding == "base64": # Characters: A-Za-z0-9 |
| 31 | hash = hashlib.sha512(os.urandom(256)).digest() |
| 32 | return base64.b64encode(hash).decode("ascii").replace("+", "").replace("/", "").replace("=", "")[0:length] |
| 33 | else: # Characters: a-f0-9 (faster) |
| 34 | return hashlib.sha512(os.urandom(256)).hexdigest()[0:length] |
| 35 | |
| 36 | |
| 37 | # Sha512 truncated to 256bits |