8

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

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Dinesh
  • 1,825
  • 5
  • 31
  • 40

2 Answers2

19

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'))
Asclepius
  • 57,944
  • 17
  • 167
  • 143
Tomáš Šíma
  • 834
  • 7
  • 26
6

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'6\x10\xa6\x86'
> base64.b16encode(digest).decode().lower()
'3610a686'
Asclepius
  • 57,944
  • 17
  • 167
  • 143