Questions/Problem
I am attempting to make a password generator that will hash the password after displayed to the user and stores it in a file that it makes the first time it is ran. I am not sure how to go about doing this or how to do this. I tested out a few password generators and ended up going with the one shown below. So far I have tried to hash with SH 256 and was unable to get that to work, along with bcrypt.
Code
from random import choice, randint
import string
characters = string.ascii_letters + string.ascii_lowercase + string.ascii_uppercase + string.digits + string.hexdigits + string.punctuation + string.octdigits
password = "".join(choice(characters) for x in range(randint(25,100)))
print(password)
this small example i hope it be useful
import random
import string
from random import choice, randint
def get_random_string(length):
characters = string.ascii_letters + string.ascii_lowercase + string.ascii_uppercase +
string.digits + string.hexdigits + string.punctuation + string.octdigits
result_str = ''.join(random.choice(characters) for i in range(length))
print("the hasheds password is:", result_str)
get_random_string(8)
get_random_string(8)
Here is a working example using hmac
with sha256
import hashlib
import hmac
import string
from random import choice, randint
characters = string.ascii_letters + string.ascii_lowercase + string.ascii_uppercase + string.digits + string.hexdigits + string.punctuation + string.octdigits
password = "".join(choice(characters) for x in range(randint(25, 100)))
print(password)
SECRET = 'this is my secret'
def hash_password(pw):
hashed_password = hmac.new(
SECRET.encode(),
msg=pw.encode(),
digestmod=hashlib.sha256
).hexdigest().upper()
return hashed_password
password_file = 'test.password'
with open(password_file, 'w') as f:
f.write(hash_password(password))
user_supplied = input('Enter the password supplied: ')
with open(password_file, 'r') as f:
print(f"Does match? {f.read() == hash_password(user_supplied)}")
Here is an example run
bXFREVkOJ~PDUBEfSkUcK[W6s~yDcDzdCi*SjxOc6T79a5[7s\P0i|)yh#'rK6nzB@CEDX1T7Umc-OOEi
Enter the password supplied: bXFREVkOJ~PDUBEfSkUcK[W6s~yDcDzdCi*SjxOc6T79a5[7s\P0i|)yh#'rK6nzB@CEDX1T7Umc-OOEi
Does match? True
This is what I use:
import hashlib
import bcrypt
import base64
import string
from random import choice
def encrypt_password(password):
password = password.encode('utf-8')
if len(password) > 72:
password = base64.b64encode(hashlib.sha256(password).digest())
return bcrypt.hashpw(password, bcrypt.gensalt()).decode('ascii') # 60 characters long
def verify_password(password, encrypted_password):
hashed = encrypted_password.encode('ascii')
password = password.encode('utf-8')
if len(password) > 72:
password = base64.b64encode(hashlib.sha256(password).digest())
return bcrypt.checkpw(password, hashed)
def generate_password(length=16, chars=string.ascii_letters+string.digits+string.punctuation):
return ''.join([ choice(chars) for i in range(length) ])
password = generate_password()
encrypted_password = encrypt_password(password)
print(password, encrypted_password)
print(verify_password(password, encrypted_password))
Prints:
SL6X95n4rk<[VHK_ $2b$12$q6DfXygpSFW3JI9EQXJmm.wy8ZhhJiJ6mK907bHJXnv8XQdEe9ofG
True
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.