Search code examples
python-3.xzlibcrc32hashlib

How do I hash key and secret message in crc32 using Python hashlib or zlib?


import hashlib
import hmac
import base64

key = b'sdfgfcxc'
secret = b'?45$dfd*632sd!'
base64.b64encode(hmac.new(key, secret, hashlib.sha512).digest())

I want to hash the same key and secret fields using crc32 algorithm. But, hashlib doesn't provide crc32 hashing. I believe we can use zlib to apply crc32 hash, but how do I combine both key and secret for getting a crc32 hash using zlib? Or does hashlib itself provides some option?


Solution

  • You can simply concatenate key and secret and use zlib.crc32() to compute the CRC-32 of that sequence of bytes.

    However, are you sure that's what you want? There's a reason that hashlib does not have a CRC-32, which is that it is easy to spoof. I.e., come up with different data that has the same hash.