Search code examples
pythonpython-3.xhashhashlib

How to turn items in an array into bytes for hashing?


I am trying to hash a user input which is entered through easygui. Easygui stores the input into an array (I think), so when I try to hash the userinput I am not sure how to turn it into a byte.

Here is my code:

import hashlib
import easygui


g = hashlib.sha256(b'helloworld').hexdigest()

l = easygui.enterbox('enter password')

f = hashlib.sha256([l]).hexdigest()

print(g)
print(f)

ideally if I type 'helloworld' into the easygui, it should return the same hashed output.

The error currently is:

"TypeError: object supporting the buffer API required" at the line f = haslib.sha256([l]).hexdigest()

Solution

  • easygui.enterbox returns the text that the user entered, or None if he cancels the operation. You will have to convert the text returned to byte array. Docs

    if l is not None:
        f = hashlib.sha256(l.encode()).hexdigest()