Home > front end >  CRC32 differs when executed on file vs text
CRC32 differs when executed on file vs text

Time:03-26

I have a file called hello.txt with following content:

hello

When I execute a linux crc32 on that file like this: (I installed via sudo apt install libarchive-zip-perl)

crc32 hello.txt

I get:

363a3020

When I want to use some online calculator or npm library (crc from node.js), I execute that on text only and get the following result:

3610a686

Which is different. How could I check this so that the results are the same ? What is the difference here ? Can someone explain ?

CodePudding user response:

It looks as if you created the file by running something like:

echo hello > hello.txt

The echo command appends a newline character, so the content of that file is actually hello<newline>. We can see that with a hexdump tool, for example:

$ od --endian=big -x hello.txt
0000000 6865 6c6c 6f0a
0000006

Or by just counting bytes:

$ wc -c hello.txt
6 hello.txt

Here we see it's 6 bytes instead of the expected 5 bytes. If we suppress the terminal newline:

echo -n hello > hello.txt

We then get the expected crc:

$ crc32 hello.txt
3610a686
  • Related