Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deterministic hashing of Python 3 strings with adler32

I read here the following:

Note: To generate the same numeric value across all Python versions and platforms use adler32(data) & 0xffffffff.

I am hoping to apply this to a string of the form: "S89234IX", but when I do so, I get:

> zlib.adler32("S89234IX")

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-84eee14d45ae> in <module>()
----> 1 zlib.adler32(campaigns_to_work_with[0])

TypeError: 'str' does not support the buffer interface

Any thoughts on how to apply this function to a string?

like image 569
Amelio Vazquez-Reina Avatar asked Feb 09 '23 23:02

Amelio Vazquez-Reina


1 Answers

data must be a byte string. If you want to compute a checksum of Unicode data, you will need to encode it to a byte string, and you will need to make sure to stick with a specific encoding. For example, using UTF-8:

checksum = zlib.adler32("S89234IX".encode('utf-8')) & 0xffffffff
like image 61
user2357112 supports Monica Avatar answered Feb 12 '23 13:02

user2357112 supports Monica