Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Calculate CRC32 checksums with cksum UNIX
Mac OS X comes with a nice commandline tool called cksum to calculate CRC16/32 checksums of files. The only downside is that the output of the CRC is in decimal only. So I made a small one-liner to convert the output to hex to make it more usable in the Internet world.

The oneliner is written for the Bourne shell (bash), and uses cksum in combination with ruby. This should be one long line; replace the line break with a single space when you enter the function:
function crc32 { cksum -o3 "$@"|ruby -e 'STDIN.each{|a|a=a.split;printf 
 "%08X\t%s\n",a[0],a[2..-1].join(" ")}'; }
Here's an example of the new function in use:
$ cd "/Library/Desktop Pictures/"
$ crc32 Aqua\ Blue.jpg 
FEDCA2D9        Aqua Blue.jpg
$ crc32 *.png
59B02128        Small Ripples graphite.png
FA28E0A8        Small Ripples.png
You can also put the function in your .bash_profile so you can use the crc32 command in all your Terminals. Of course there are existing CRC32/SFV tools and this is not to replace them, but this is a nice alternative making use of the standard tools that come with the OS.
    •    
  • Currently 4.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[26,764 views]  

Calculate CRC32 checksums with cksum | 9 comments | Create New Account
Click here to return to the 'Calculate CRC32 checksums with cksum' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Calculate CRC32 checksums with cksum
Authored by: firstianus@yahoo on Dec 28, '04 11:46:54AM

An even simple way of doing it is to use awk instead:

function crc32 { cksum -o3 "$@" | awk '{printf("0x%x\n", $1)}'; }



[ Reply to This | # ]
Calculate CRC32 checksums with cksum
Authored by: fracai on Dec 28, '04 01:45:30PM

doesn't work for me. It looks like it hits an overflow at 2147483647 or 0x7fffffff

does awk have weird ceilings?

---
i am jack's amusing sig file



[ Reply to This | # ]
Calculate CRC32 checksums with cksum
Authored by: guybrush on Dec 28, '04 03:26:48PM

I did use awk first for this purpose but seemed it has a max integer limit at 0x7FFFFFFF, ie 2147483647..

Couldnt find how to solve this problem so I used ruby instead :)



[ Reply to This | # ]
Calculate CRC32 checksums with cksum
Authored by: bdm on Dec 29, '04 05:45:56PM

Within limits you can trick awk past its integer maximum:

cksum -o3 something | awk '{printf("0x%x%02x\n", $1/256,$1%256)}'

If I remember correctly, awk does its arithmetic using doubles.

Brendan.



[ Reply to This | # ]
Calculate CRC32 checksums with cksum
Authored by: bdm on Dec 29, '04 06:58:23PM

Sorry, that should be

... | awk '{printf("0x%x%02x\n", int($1/256),$1%256)}'

to make sure that the truncation to integer is correct.

Brendan.



[ Reply to This | # ]
Calculate CRC32 checksums with cksum
Authored by: LC on Dec 28, '04 08:15:53PM
I usually use bc with obase = 16, for this;
perl ought to be an option too.

[ Reply to This | # ]
Calculate CRC32 checksums with cksum
Authored by: topsnark on Dec 28, '04 08:41:21PM
checkSum+ from http://homepage.mac.com/julifos/soft/checksum/ is a frontend for this and other checksum algorithms. It, too, outputs in hex, using cksum and awk. I'm not sure exactly how, but I think it has avoided such a problem, because I have successfully used it for files large and small.

[ Reply to This | # ]
perl version
Authored by: SOX on Dec 29, '04 12:03:42AM
In perl, with some extra features:
cksum test.file | perl -wane 'printf "%0x %d %s\n",@F'

replace test.file with any filename, or any list of files

for example to apply this to every file in the current directory:

cksum ./* | perl -wane 'printf "%0x %d %s\n",@F'



[ Reply to This | # ]

Calculate CRC32 checksums with cksum
Authored by: mweissen on Dec 30, '04 03:05:58AM

I always use openssl md5 instead. On machines with the Debian package manager installed (i.e. fink) there is also an md5sum command.

$ openssl md5 wxMac-2.5.3.tar.gz
MD5(wxMac-2.5.3.tar.gz)= 0dad0a8ac9657662f2543c567314a6e7
$ md5sum wxMac-2.5.3.tar.gz
0dad0a8ac9657662f2543c567314a6e7 wxMac-2.5.3.tar.gz

--Bud



[ Reply to This | # ]