home *** CD-ROM | disk | FTP | other *** search
/ Freelog 33 / Freelog033.iso / Progr / Python-2.2.1.exe / MD5SUM.PY < prev    next >
Encoding:
Python Source  |  2001-06-22  |  711 b   |  33 lines

  1. #! /usr/bin/env python
  2.  
  3. """Python utility to print MD5 checksums of argument files.
  4.  
  5. Works with Python 1.5.2 and later.
  6. """
  7.  
  8. import sys, md5
  9.  
  10. BLOCKSIZE = 1024*1024
  11.  
  12. def hexify(s):
  13.     return ("%02x"*len(s)) % tuple(map(ord, s))
  14.  
  15. def main():
  16.     args = sys.argv[1:]
  17.     if not args:
  18.         sys.stderr.write("usage: %s file ...\n" % sys.argv[0])
  19.         sys.exit(2)
  20.     for file in sys.argv[1:]:
  21.         f = open(file, "rb")
  22.         sum = md5.new()
  23.         while 1:
  24.             block = f.read(BLOCKSIZE)
  25.             if not block:
  26.                 break
  27.             sum.update(block)
  28.         f.close()
  29.         print hexify(sum.digest()), file
  30.  
  31. if __name__ == "__main__":
  32.     main()
  33.