Wednesday, April 15, 2009
Ubuntu's missing crc32sum tool, in Python
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 pythonDon't say I've never done anything for you.
#
# A CRC32 summing utility that functions mostly like md5sum or
# sha1sum.
#
# Public domain by Matt Behrens <matt@zigg.com> 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)
Labels: code
0 Comments:
Further comments are disabled indefinitely. Here's why.
