-1
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!

P.S. 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.

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 1
    There's a lot of weird stuff going on here. Why are you doing `"" + uname `? Are you coming from Javascript? Python is a strongly typed language, and these sorts of operations would throw an error with an incompatible type, not coerce it into a string. Basically, `users.update({"" + uname : "" + pword})` should just be `users[uname] = pword` – juanpa.arrivillaga Feb 14 '20 at 03:15
  • 2
    Instead of naively use `str` to cast the `hash_object`, please use `hash_object.hexdigest()` instead. [Some additional basics information](https://stackoverflow.com/questions/4820043/basics-of-python-encryption-w-hashlib-sha1#48202630) – metatoaster Feb 14 '20 at 03:15
  • Also, "I know that storing a username and password in a dictionary is not efficient", efficient compared to what? What is so inefficient about it? – juanpa.arrivillaga Feb 14 '20 at 03:17

1 Answers1

2

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

hash_object.hexdigest()
# 'dc7e6b79b489fa810e11889710023e7e2e36fb64'
DYZ
  • 55,249
  • 10
  • 64
  • 93