Command to verify CRC (CRC32) hashes recursively

5,081

Solution 1

Try RHash

Try RHash.

There are packages for Cygwin, Debian.

Example

$ echo -n a > a.txt; echo -n b > b.txt; echo -n c > c.txt
✔

$ rhash --crc32 --simple *.txt > checksums.crc32
✔

$ cat checksums.crc32
e8b7be43  a.txt
71beeff9  b.txt
06b9df6f  c.txt
✔

$ rhash --crc32 --check checksums.crc32
--( Verifying checksums.crc32 )-------------------------------------------------
a.txt                                               OK
b.txt                                               OK
c.txt                                               OK
--------------------------------------------------------------------------------
Everything OK
✔

Note 1: --simple format

If you don't use the --simple formatting option then rhash will default to a different format. And this may not be what you want:

$ rhash --crc32 *.txt
; Generated by RHash v1.3.7 on 2020-06-03 at 16:02.51
; Written by Kravchenko Aleksey (Akademgorodok) - http://rhash.sf.net/
;
;            1  15:58.36 2020-06-03 a.txt
;            1  15:58.36 2020-06-03 b.txt
;            1  15:58.36 2020-06-03 c.txt
a.txt E8B7BE43
b.txt 71BEEFF9
c.txt 06B9DF6F
✔

Note 2: --all option

If you wanna go crazy: try the --all option to get ALL supported hashes at once.

Solution 2

The crc32 utility does not have an option like -c of the sha1sum. Nevertheless the verification can be done in a script.

Let say we have stored crc32 values in file in the same format like sha1sum prints values:

a8374911 *file1.ext
32c5188e *file2
6592d5e5 *bflmpsvz

The crc32 can be checked via bash script:

while read line
do 
   name=${line#* \*}
   crc32=`crc32 "$name"`
   echo -n "$name: "
   if [ "${line% \**}" == "$crc32" ]
   then
      echo OK
   else
      echo FAILED
   fi
done < file_with_crc32

The script provides the same output as sha1sum -c. It iterates over all lines in the file file_with_crc32 and for each line

  • retrieves the filename from read line
  • calculates crc32
  • compares the calculated value with the value from file
  • prints the result
Share:
5,081

Related videos on Youtube

Nemo
Author by

Nemo

Updated on September 18, 2022

Comments

  • Nemo
    Nemo almost 2 years

    With the commands md5sum, sha1sum, sha256sum I can take a text file having an hash and a path per line and verify the entire list of files in a single command, like sha1sum -c mydir.txt. (Said text file is easy to produce with a loop in find or other.)

    Is there a way to do the same with a list of CRC/CRC32 hashes?

    Such hashes are often stored inside zip-like archives, like ZIP itself or 7z. For instance:

    $ unzip -v archive.zip 
    Archive:  archive.zip
     Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
    --------  ------  ------- ---- ---------- ----- --------  ----
     8617812  Stored  8617812   0% 12-03-2015 15:20 13fda20b  0001.tif
    

    Or:

    $ 7z l -slt archive.7z
    
    Path = filename
    Size = 8548096
    Packed Size =
    Modified = 2015-12-03 14:20:20
    Attributes = A_ -rw-r--r--
    CRC = B2F761E3
    Encrypted = -
    Method = LZMA2:24
    Block = 0
    
    • Nemo
      Nemo over 8 years
    • Nemo
      Nemo over 8 years
      Yes, I'm aware of askubuntu.com/q/303662/395065
    • roaima
      roaima over 8 years
      It's unlikely that there'll be a command to verify CRC hashes recursively. What would be more usual is for two or more commands to be combined to deliver the target requirement. There'll be answers to "How do I run a command recursively", and "How do I extract a particular value from a list", I'm sure.
    • Nemo
      Nemo over 8 years
      Sure, but that's not the question. Here we are looking at a specific feature of the various hashing options. 7z h is a recent example of tool for recursive hashing in CRC32 and more.
  • Nemo
    Nemo about 4 years
    Excellent! It's packaged in Fedora too. I switched the accepted answer to this.