3

I am trying to create a login system for my project and I need to use hashing for the users password, I am unsure how to hash a variable name and convert it into bytes to give a hex result for the password.

tried using:

hash_object = hashlib.md5(b(password))

and:

hash_object = hashlib.md5((password))

Code:

import hashlib

user = {}

username = input("What is your username? ")
password = input("What is your password? ")

hash_object = hashlib.md5((password))
print(hash_object.hexdigest())

Error:

Traceback (most recent call last):
  File "E:\loginsystem.py", line 8, in <module>
    hash_object = hashlib.md5((password))
TypeError: Unicode-objects must be encoded before hashing
khelwood
  • 55,782
  • 14
  • 81
  • 108
Cryces
  • 37
  • 1
  • 1
    also - do not use md5 to hash passwords. see here: https://security.stackexchange.com/questions/211/how-to-securely-hash-passwords/31846#31846 for better recommendations. – modesitt Apr 14 '19 at 21:40
  • 2
    If you're just learning about security - fine. However if you're ever going to publish something to the public internet / actual users then do not roll your own authentication - leave it to the experts. – eddiewould Apr 14 '19 at 21:44
  • 1
    so its utf8 rightttt lol – Cryces Apr 14 '19 at 21:50

1 Answers1

1

Encode the password string with the .encode method.

import hashlib

user = {}

username = input("What is your username? ")
password = input("What is your password? ")

hash_object = hashlib.md5(passsword.encode('utf8'))
print(hash_object.hexdigest())

I recommend this great thread that might clear some things up:

What is the difference between a string and a byte string?

Joe Iddon
  • 20,101
  • 7
  • 33
  • 54