1

I am having trouble using sha256 hash for a variable. Here is my code:

var = 'password'
hashedWord = sha256(b var).hexdigest()
print(hashedWord)

I know it would be easier to do this:

hashedWord = sha256(b'password').hexdigest()
print(hashedWord)

But I don't want to do it that way. Can anyone help?

0

2 Answers 2

4

You need to encode strings to bytes:

var = 'password'
hashedWord = sha256(var.encode('ascii')).hexdigest()

Pick an encoding that works for your text; UTF-8 can encode all of Unicode but that may not produce the hash signature you are looking for; this depends on what other systems think the signature is.

Sign up to request clarification or add additional context in comments.

Comments

1

An alternative to Martijn's solution would be to store a byte string in the var variable.

var = b'password' hashedWord = sha256(var).hexdigest() print(hashedWord)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.