From: Vincent Le Gallic Date: Sun, 29 Sep 2013 17:01:49 +0000 (+0200) Subject: Pour md5sumer plein de fichiers X-Git-Url: http://gitweb.pimeys.fr/?a=commitdiff_plain;h=99f7ef462151c69272248a38eaaa0dc12193c38b;p=scripts-20-100.git Pour md5sumer plein de fichiers --- diff --git a/md5sum.py b/md5sum.py new file mode 100755 index 0000000..f26d29a --- /dev/null +++ b/md5sum.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +## trouvé sur http://code.activestate.com/recipes/266486/ + +## md5hash +## +## 2004-01-30 +## +## Nick Vargish +## +## Simple md5 hash utility for generating md5 checksums of files. +## +## usage: md5hash [..] +## +## Use '-' as filename to sum standard input. + +import hashlib +import sys + +def sumfile(fobj): + '''Returns an md5 hash for an object with read() method.''' + m = hashlib.md5() + while True: + d = fobj.read(8096) + if not d: + break + m.update(d) + return m.hexdigest() + + +def md5sum(fname): + '''Returns an md5 hash for file fname, or stdin if fname is "-".''' + if fname == '-': + ret = sumfile(sys.stdin) + else: + try: + f = file(fname, 'rb') + except: + return 'Failed to open file' + ret = sumfile(f) + f.close() + return ret + + +# if invoked on command line, print md5 hashes of specified files. +if __name__ == '__main__': + for fname in sys.argv[1:]: + print '%32s %s' % (md5sum(fname), fname) + + +