Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python find CRC32 of string

Tags:

python

crc32

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

like image 242
Dinesh Avatar asked Nov 17 '17 02:11

Dinesh


2 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'))
like image 59
Tomáš Šíma Avatar answered Oct 01 '22 23:10

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, by using & 0xffffffff for reproducible output, and by producing a string output besides just an integer.

> import binascii, zlib

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

# Using binascii
> crc32 = binascii.crc32(data) & 0xffffffff  # Must use "& 0xffffffff" as per docs.
> crc32
907060870
> hex(crc32)
'0x3610a686'
> f'{crc32:#010x}'
'0x3610a686'

# Using zlib
> zlib.crc32(data) & 0xffffffff  # Must use "& 0xffffffff" as per docs.
907060870  # Works the same as binascii.crc32.

# In one line:
> hex(binascii.crc32(text.encode()) & 0xffffffff)
'0x3610a686'
like image 37
Asclepius Avatar answered Oct 01 '22 23:10

Asclepius