Compute checksum on file from command line

2.7k views Asked by At

Looking for a command or set of commands that are readily available on Linux distributions that would allow me to create a script to generate a checksum for a file.

This checksum is generated by a build system I have no control over by summing every single byte in the file and then truncating that number to 4 bytes.

I know how to do do this using tools like node.js, perl, python, C/C++, etc, but I need to be able to do this on a bare bones Linux distribution running remotely that I can't modify (it's on a PLC).

Any ideas? I've been searching for awhile and haven't found anything that looks straightforward yet.

3

There are 3 answers

1
Pratap On BEST ANSWER

The solution for byte by byte summation and truncating that number to 4 bytes using much primitive shell commands.

#! /usr/bin/env bash

declare -a bytes
bytes=(`xxd -p -c 1 INPUT_FILE | tr '\n' ' '`)

total=0;
for(( i=0; i<${#bytes[@]}; i++));
do
    total=$(($total + 0x${bytes[i]}))
    if [ $total > 4294967295 ]; then
            total=$(($total & 4294967295))
    fi
done

echo "Checksum: " $total
3
Gustavo Baseggio On

Have you tried cksum? I use it inside a few scripts. It's very simple to use.

http://linux.die.net/man/1/cksum

5
Pratap On

If you just want to do byte by byte summation and truncating that number to 4 bytes then the following command can be used.

xxd -p -c 1 <Input file> | awk '{s+=$1; if(s > 4294967295) s = and(4294967295, s) } END {print s}'

The xxd command is used extract hexdump of the input file and each byte is added to compute the sum. If the sum exceeds 2^32-1 = 4294967295 value, then a bitwise and operation is performed to truncate the bits.