[code.view]

[top] / python / PyMOTW / hashlib / hashlib_update.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """Calling update() more than once is like calling it with all of the data at once.
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     import hashlib
     
     from hashlib_data import lorem
     
     h = hashlib.md5()
     h.update(lorem)
     all_at_once = h.hexdigest()
     
     def chunkize(size, text):
         "Return parts of the text in size-based increments."
         start = 0
         while start < len(text):
             chunk = text[start:start+size]
             yield chunk
             start += size
         return
     
     h = hashlib.md5()
     for chunk in chunkize(64, lorem):
         h.update(chunk)
     line_by_line = h.hexdigest()
     
     print 'All at once :', all_at_once
     print 'Line by line:', line_by_line
     print 'Same        :', (all_at_once == line_by_line)
     

[top] / python / PyMOTW / hashlib / hashlib_update.py

contact | logmethods.com