CRC32 Checksum on Linux/OS X


Krade wrote this in the early evening:

If you’re a weaboo, odds are that you have tons of animu laying around in your Hard Disk. Many (if not most) fansubbing groups append a hex number to the filename of their releases — this a CRC32 checksum, a checksum calculated from the contents of your file. If your file somehow got corrupted or is a incomplete download or whatever, the CRC checksum you calculate from that file isn’t going to match the one on the filename. This is usually accomplished by checking md5 hashes instead, but md5 hashes are way too big to fit in a (sane) filename.

I watch my animu on a Mac (yay for Perian) because I really like the lappy screen and Front Row is quite nice. However, I couldn’t find a way to easily calculate a CRC checksum on a Mac in the same way Windows does it, so I wrote a bash function to help me calculate those values:

function crc()
{
for i in "$@"; do
    echo -n "$i = "
    echo -e "obase=16n`cksum -o 3 "$i" |
        sed 's/([0-9][0-9]*) .*/1/'`"  | bc
done
}

This works for both Linux, OS X and *insert favorite *NIX flavor with a somewhat recent bash shell installed*. Just paste that in your ~/.bash_profile (or .bashrc) and then you can use your terminal to calculate checksums just by typing crc <filename>. Wildcards work, too! Example:

miguel@singularity ~/Movies/Lucky☆Star $ crc *
[EnA]_Lucky_Star_01_(1024x576_h.264)_[C5D5FB67].mkv = C5D5FB67
[EnA]_Lucky_Star_02_(1024x576_h.264)_[1F2C5B56].mkv = 1F2C5B56
[EnA]_Lucky_Star_03_(1024x576_h.264)_[39C496E1].mkv = 39C496E1
[EnA]_Lucky_Star_04_(1024x576_h.264)_[67B524C7].mkv = 67B524C7
[EnA]_Lucky_Star_05_(1024x576_h.264)_[D1C60977].mkv = D1C60977

Yay for non-corrupted downloads. o/

3 Responses to “CRC32 Checksum on Linux/OS X”

  1. Bummed Says:

    Too bad this doesn’t work with the shell I use. No -o option. It looked so promising, too…

    Thanks regardless,
    Greg

    Using Firefox Firefox 2.0.0.11 on Windows Windows XP
  2. Krade Says:

    To be fair, I wrote that before I found out that there’s usually already a program called crc32 around on most Unix-like systems and I haven’t been bothered to update this post. At least on OS X and CentOS, /usr/bin/crc32 exists and displays the correct result. On the Gentoo installation I have access to, there is none, but the sysadmins of that server are dumbasses.

    The cksum program I used in my original post also seems to be FreeBSD / OS X specific (it behaves differently on other systems), so it isn’t as cross-platform as I thought I was.

    So yeah, you could just do crc32 * in the command line and it would work. Or, if you prefer the CRC hex number to be displayed in all caps:

    function crc()
    {
    crc32 "$@" | awk 'BEGIN { FS = "\t" } ; { print toupper($1), "\t", $2 }'
    }

    That should work fine.

    Using Mozilla Mozilla 1.9b3pre on Mac OS Mac OS X
  3. Scott Says:

    I found this post helpful, to find crc32. Info is scarce for some reason… I found it comes with the package “libarchive-zip-perl”.

    Using Firefox Firefox 3.0.1 on Linux Linux

Leave a Reply