In Python 2, one could hash a string by just running:
someText = "a"
hashlib.sha256(someText).hexdigest()
But in Python 3, it needs to be encoded:
someText = "a".encode("ascii")
hashlib.sha256(someText).hexdigest()
But when I try this with a file:
f = open(fin, "r")
sha = hashlib.sha256()
while True:
data = f.read(2 ** 20).encode("ascii")
if not data:
break
sha.update(data)
f.close()
I get this on many files:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 8: invalid continuation byte
I assume this is because it's a binary file, which likely can't be converted to ASCII.
How can I encode the file without this problem?