User Hash Generator
User Hash Generator
I used this script in a flask website to use another identifier beside user id. It creates hashes with very low possibility of collusion. You may consider its other use cases.
Getting Started
To demonstrate generator, a working terminal program is integrated. To try, type in terminal:
$ python3 hash.py
Notice: ‘python3’ keyword may be ‘py’ or ‘python’ for your system. \
To use in your own project, examine the code.
Source Code: hash.py
from time import time
from random import randint
def user_hash_generator():
n = 1
while True:
timestamp = int(time()*1000)
hash_string = hex(hash((timestamp/(randint(1, 250)+n))
* (randint(1, 10)*5*n)))[2:14]
n += 1
yield hash_string
if __name__ == '__main__':
print('Generator will create a random hash when you press only Enter.\
\nTo exit, press any other button then Enter.')
hasher = user_hash_generator()
while True:
ch = input()
if ch == '':
print(next(hasher))
else:
print('Terminated')
break