MD5 checksum for a file ?

Oleg Broytmann phd at
Wed Jan 23 06:05:02 EST 2002

On Wed, Jan 23, 2002 at 11:47:27AM -0000, Barghest wrote:
> Somebody have a sample script to make a md5 from a file or to check if the
> md5 is equal ??

   Attached. Well, it is not exactly script...

     Oleg Broytmann              phd at
           Programmers don't die, they just GOSUB without RETURN.
-------------- next part --------------
#! /usr/local/bin/python
   User wrapper for md5 builtin object

   Written by BroytMann, Jun 1997. Copyright (C) 1997 PhiloSoft Design

from md5 import md5

class md5wrapper:
   def __init__(self, init=None):
      if init:
         self._md5 = md5(init)
         self._md5 = md5()

   def update(self, data):

   def digest(self):
      return self._md5.digest()

   def __repr__(self):
      str = self.digest()
      return "%02x"*len(str) % tuple(map(ord, str))
         # This nice was suggested by Guido

   def md5file(self, f):
      if type(f) == type(''): # If f is string - use it as file's name
         infile = open(f, 'r')
         infile = f           # else assume it is file or file-like object

         while 1:
            buf =*1024)
            if not buf: break

         if type(f) == type(''): # If f was opened - close it

if __name__ == "__main__":
   print "This must print exactly the string"
   print "Test: 900150983cd24fb0d6963f7d28e17f72"
   print "Test:", md5wrapper("abc")

More information about the Python-list mailing list