Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate crc32 checksum from a string on linux bash

I used crc32 to calculate checksums from strings a long time ago, but I cannot remember how I did it.

echo -n "LongString" | crc32    # no output

I found a solution [1] to calculate them with Python, but is there not a direct way to calculate that from a string?

# signed
python -c 'import binascii; print binascii.crc32("LongString")'
python -c 'import zlib; print zlib.crc32("LongString")'
# unsigned
python -c 'import binascii; print binascii.crc32("LongString") % (1<<32)'
python -c 'import zlib; print zlib.crc32("LongString") % (1<<32)'

[1] How to calculate CRC32 with Python to match online results?

like image 781
oxidworks Avatar asked Jun 28 '17 13:06

oxidworks


People also ask

How do you find the CRC of a string?

The crc32() function calculates a 32-bit CRC (cyclic redundancy checksum) for a string. This function can be used to validate data integrity. Tip: To ensure that you get the correct string representation from the crc32() function, you'll need to use the %u formatter of the printf() or sprintf() function.

How do I find the CRC32 file in Linux?

cksfv app from cksfv package generates CRC32 checksum as well. could you give a usage example? cksfv -c "file" prints the CRC32 to stdout. If you want to suppress the header, a cksfv -c "file" 2>/dev/null | grep -v ^\; gives the filename + CRC32 and no warning for a directory.

How is CRC32 calculated?

The most common variant of the CRC32 checksum, sometimes called CRC-32b, is based on the following generator polynomial: g(x) = x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1. This code processes one bit at a time.

How is CRC calculated in Linux?

This command provides a feature where-in the user can type just 'cksum' or 'cksum-' and write on stdin and then press Ctrl+D couple of times. This way cksum gives the checksum of the data entered at the input. In the example above, we actually calculated the checksum of the string “Lets check the checksum”.


1 Answers

Or just use the process substitution:

crc32 <(echo "LongString")
like image 93
C Würtz Avatar answered Sep 18 '22 08:09

C Würtz