1

I wanted to make SHA256 hasher that will save both input and output in text file. I searched stackoverflow for answers but it didn't work. I wanted it to write in the text file:

all = str("At: " + date + " you have encrypted: " + text + " into:" + hex_dig)
text_file.write(together)

While the date looks like that:

date = time.strftime("%Y-%m-%d %H:%M:%S")

It gave me this error in the first line of my sample: TypeError: Can't convert 'bytes' object to str implicitly.

2

3 Answers 3

4

I'm guessing that hex_dig is a bytes object (did you use digest rather than hexdigest to get the hash perhaps?). If that's the case, simply using the right function will sort this out:

sha256_hasher = hashlib.sha256()
sha256_hasher.update(your_data_goes_here)
hex_dig = sha256_hasher.hexdigest()

Otheriwse, and more generally, you're trying to concatenate strs and bytes objects together. You can't do that. You need to convert the bytes object to a string. If it just contains text data you can decode it:

hex_dig = hex_dig.decode("ascii")

Or, if it just contains bytes and you want to see the hex you can use binascii.hexlify (you'll still need to decode as it returns a bytes):

import binascii
hex_dig = binascii.hexlify(hex_dig).decode("ascii")

As an aside, you don't need to wrap a string in the str function call, you only need that if you want to get the string representation of an object that isn't already a string. What you have (or what you want) is already a string so it's a redundant call. You can't try to concatenate things of different types and wrap all that in a str cal and hope python will sort it out for you - it won't (and shouldn't as it's ambiguous - explicit is better than implicit).

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

Comments

0
date = time.strftime("%Y-%m-%d %H:%M:%S")
all = "At: %s you have encrypted: %s into: %s" % (date, text, hex_dig)
text_file.write(all)

4 Comments

the str call around hex_dig is redundant if you're using a %s format.
Is there a b'your_data_here' around your hash though?
@LexyStardust I think digests get generated as byte arrays rather than strings
In Py3k (assuming they're using hashlib) the digest function returns a bytes; hexdigest returns a string (docs.python.org/3/library/hashlib.html#hashlib.hash.hexdigest) I was assuming from the variable name chosen that it was hexdigest that was called (though with so little code to go on, that was only a guess).
0

I highly recommend the format method for cases like this.

You wrote:

all = str("At: " + date + " you have encrypted: " + text + " into:" + hex_dig)

In format syntax, it would look like this:

all = "At: {} you have encrypted: {} into:{}".format(date,text,hex_dig)

The brilliant thing about format is that it handles conversion of various data types to string (usually) with great ease.

Hint: you can set your {} with {0} {1} {2}, etc. if you want to specify which order and/or repeat place holders.

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.