Ubuntu's missing crc32sum tool, in Python
April 15, 2009 at 2:24 PM (UTC)
Did I hear you say "I want a crc32sum application for Linux that can not only output CRC32 sums for a list of files, but also handle stdin if I leave its argument list blank, and I don't mind if it's hackish, so long as it works?"
#!/usr/bin/env python
#
# A CRC32 summing utility that functions mostly like md5sum or
# sha1sum.
#
# Public domain by Mattie Behrens <[email protected]> with NO WARRANTY.
#
# Usage: crc32sum [<filename> [<filename>...]]
#
BLOCKSIZE = 0x10000
import sys
from zlib import crc32
if sys.argv[1:]:
names = sys.argv[1:]
else:
names = [None]
for name in names:
if name is not None:
f = open(name, 'rb')
else:
f = sys.stdin
name = '-'
crc = None
while 1:
data = f.read(BLOCKSIZE)
if data:
if crc is None:
crc = crc32(data)
else:
crc = crc32(data, crc)
else:
break
print '%08x\t%s' % ((crc & 0xffffffff), name)
Don't say I've never done anything for you.