2

I wanted to use sha1 alghoritm to calculate the checksum of some data, the thing is that in python hashlib input is given as string.

Is it possible to calculate sha1 in python, but somehow give raw bytes as input?

I am asking because if I would want to calculate hash of an file, in C I would use openssl library and just pass normal bytes, but in Python I need to pass string, so if I would calculate hash of some specific file I would get different results in both languages.

3
  • Well, you could convert the text in the file to ascii and fire it through hashlib. I only think it makes difference for encodings other than single-byte. Commented Mar 10, 2013 at 16:11
  • I done some more reading and after viewing this question:stackoverflow.com/questions/2672326/… i think I can use struct module to build this byte-string representation of anything and pass it to hashlib. Correct me if I am wrong Commented Mar 10, 2013 at 16:14
  • Unless you know for certain that you are using a multi-byte charset in the file you want to hash, you can just pass it to the hashing function like you'd do in C. Commented Mar 10, 2013 at 16:16

1 Answer 1

5

In Python 2.x, str objects can be arbitrary byte streams. So yes, you can just pass the data into the hashlib functions as strs.

>>> import hashlib
>>> "this is binary \0\1\2"
'this is binary \x00\x01\x02'
>>> hashlib.sha1("this is binary \0\1\2").hexdigest()
'17c27af39d476f662be60be7f25c8d3873041bb3'
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, knowing this I can also use struct module to compute hash of for example for 4 byte integer.
@Andna you could do that with your old C hashing library too. I'm not sure why you'd involve the struct module for any of this. Can you elaborate?
Are you asking what I want to do this in Python?
No you cannot. Try hashlib.sha1(u'\xcc\x88u') and see.
@SassaNF that's because the u'...' causes the string to be a unicode object instead of a str object. To turn a unicode object into a str, you'll need to encode it. e.g. hashlib.sha1(u'\xcc\x88u'.encode("utf-8"))
|

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.