Created
July 26, 2018 06:34
-
-
Save reganto/6fa0fb1bbb6ddbc32f49c54ae9c2ea54 to your computer and use it in GitHub Desktop.
simple gen and hash password
This file contains 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 string | |
import random | |
import uuid | |
import hashlib | |
import os | |
def gen_password(): | |
print("Gen Password . . .\n") | |
try: | |
password = input("Enter a password or Enter . to generate one :") | |
if password == '.': | |
char = string.ascii_letters + string.digits + string.punctuation | |
new_password = ''.join((random.choice(char)) for i in range(random.randint(5,5))) | |
print("generated password is {0}".format(new_password)) | |
else: | |
print("your entered password is {0}".format(password)) | |
except ValueError: | |
print('\nError\n{0}'.format(ValueError)) | |
else: | |
print("\nwell done\n") | |
finally: | |
print("\n------------------------------------------------") | |
return password | |
def hash_password(password): | |
print("Hash Password . . .\n") | |
try: | |
salt = uuid.uuid4().hex.encode('utf-8') | |
password = password.encode('utf-8') | |
hashed_password = hashlib.sha512(password + salt).hexdigest() | |
# | |
salt = salt.decode() | |
salt = str(salt) | |
hashed_password = str(hashed_password) | |
print('salt : {0}\nhashed password : {1}'.format(salt, hashed_password)) | |
with open('file', 'a') as f: | |
f.write("Salt : {0}\n Hashed Password : {1}\n".format(salt, hashed_password)) | |
except ValueError: | |
print('ERROR {0}'.format(ValueError)) | |
else: | |
print('\nwell done\n') | |
finally: | |
print("\n------------------------------------------------") | |
def main(): | |
password = gen_password() | |
pid = os.fork() | |
if pid == 0: | |
hash_password(password) | |
os._exit(0) | |
else: | |
ret_pid, status = os.wait() | |
if status == 0: | |
print("\n\nProgram exit seccesfuly\n\n") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment