This is a program that generates a random hex value.
# How this works:
# uses random to get a random number
# checks what hash method to use
# uses that method to hash the random number
# returns the value
import hashlib
import random as rand
def hexrandom(minint, maxint, shamode="sha1"):
x = str(rand.randint(int(minint), int(maxint)))
reval = None
if shamode == "sha1":
reval = hashlib.sha1(x.encode()).hexdigest()
elif shamode == "sha224":
reval = hashlib.sha224(x.encode()).hexdigest()
elif shamode == "sha256":
reval = hashlib.sha256(x.encode()).hexdigest()
elif shamode == "sha384":
reval = hashlib.sha384(x.encode()).hexdigest()
elif shamode == "sha512":
reval = hashlib.sha512(x.encode()).hexdigest()
return reval
How it works is simple.
It creates a random number.
It then hashes that number.
Then, it .hexdigest()
s that.
And then returns that.
By the way, I tried being descriptive with explaining it, but it kept saying that there was unformated code. Sorry about that. The big description will be in a .md
file on the repository. MarkDown file here
Development of the module this will be here.
randint
internally gets 4 bytes to make an integer out of. Why not just get however many bytes you need (stackoverflow.com/questions/5495492/…) and then encode as hex if necessary? Additionally Python 3.6 hassecrets.token_hex
: docs.python.org/3/library/secrets.html#secrets.token_hex \$\endgroup\$if
chain has already been given, but the much more helpful advice to use a different algorithm has not. \$\endgroup\$