Skip to content

Instantly share code, notes, and snippets.

@EnigmaCurry
Created October 5, 2010 03:10
Show Gist options
  • Save EnigmaCurry/610923 to your computer and use it in GitHub Desktop.
Save EnigmaCurry/610923 to your computer and use it in GitHub Desktop.
import md5
import sys
def sumfile(fobj):
'''Returns an md5 hash for an object with read() method.'''
m = md5.new()
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment