简体   繁体   中英

Creating a text file that stores a hashed password in Python

import os
import time
import random
import sys
import string
import hashlib

users = {}

class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password

running = True

while running:
    os.system("cls")
    print("Welcome. Please register yourself.")
    print()
    uname = input("Username: ")
    pword = input("Password: ")

    users.update({"" + uname : "" + pword})

    hash_object = hashlib.sha1(pword.encode())

    pswrd = open("hash.txt", "a")
    pswrd.write(str(hash_object))

    pswrd.close()

    for key, value in users.items():
        print()
        print(key, " : ", value)
        time.sleep(3)

When I open the text file, it has this:

<sha1 HASH object @ 0x010E6600>

How can I prevent this? Any help is greatly appreciated!

PS I know that storing a username and password in a dictionary is not efficient, but it's the only way I can do it right now.

You probably want to store not the hash itself, but its HEX digest:

hash_object.hexdigest()
# 'dc7e6b79b489fa810e11889710023e7e2e36fb64'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM