Python find CRC32 of string

Question:

I tried to get crc32 of a string data type variable but getting the following error.

>>> message='hello world!'
>>> import binascii
>>> binascii.crc32(message)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

For a string values it can be done with binascii.crc32(b'hello world!') but I would like to know how to do this for a string data-type variable

Asked By: Dinesh

||

Answers:

When you are computing crc32 of some data, you need to know the exact value of bytes you are hashing. One string can represent different values of bytes in different encodings, therefore passing string as parameter is ambiguous.

When using binascii.crc32(b'hello world!'), you are converting char array into array of bytes using simple ascii table as conversion.

To convert any string, you can use:

import binascii

text = 'hello'
binascii.crc32(text.encode('utf8'))
Answered By: Tomáš Šíma

This can be done using binascii.crc32 or zlib.crc32. This answer improves upon the prior answer by Tomas by documenting both modules and by producing a string output besides just an integer.


# Define data
> text = "hello"
> data = text.encode()
> data
b'hello'

# Using binascii
> import binascii
> crc32 = binascii.crc32(data)
> crc32
907060870
> hex(crc32)
'0x3610a686'
> f'{crc32:#010x}'
'0x3610a686'

# Using zlib
> import zlib
> zlib.crc32(data)
907060870  # Works the same as binascii.crc32.

If you don’t want the string output to have the 0x prefix:

> import base64
> crc32 = 907060870
> digest = crc32.to_bytes(4, 'big')
> digest
b'6x10xa6x86'
> base64.b16encode(digest).decode().lower()
'3610a686'
Answered By: Asclepius
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.