Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8951

Re: Does hashlib support a file mode?

From Peter Otten <__peter__@web.de>
Subject Re: Does hashlib support a file mode?
Date 2011-07-06 18:26 +0200
Organization None
References <453cc47b-b545-4bcf-91c0-2efe2aae37bd@v11g2000prn.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.704.1309969573.1164.python-list@python.org> (permalink)

Show all headers | View raw


Phlip wrote:

> Tx, all!. But...
> 
>> For example I use this function to copy a stream and return a SHA512 and
>> the output streams size:
>>
>> def write(self, in_handle, out_handle):
>> m = hashlib.sha512()
>> data = in_handle.read(4096)
>> while True:
>> if not data:
>> break
>> m.update(data)
>> out_handle.write(data)
>> data = in_handle.read(4096)
>> out_handle.flush()
>> return (m.hexdigest(), in_handle.tell())
> 
> The operation was a success but the patient died.
> 
> My version of that did not return the same hex digest as the md5sum
> version:
> 
> 
> def file_to_hash(path, m = hashlib.md5()):
> 
>     with open(path, 'r') as f:
> 
>         s = f.read(8192)
> 
>         while s:
>             m.update(s)
>             s = f.read(8192)
> 
>     return m.hexdigest()
> 
> You'll notice it has the same control flow as yours.
> 
> That number must eventually match an iPad's internal MD5 opinion of
> that file, after it copies up, so I naturally cannot continue working
> this problem until we see which of the two numbers the iPad likes!

- Open the file in binary mode.
- Do the usual dance for default arguments:
    def file_to_hash(path, m=None):
        if m is None:
            m = hashlib.md5()

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Does hashlib support a file mode? Phlip <phlip2005@gmail.com> - 2011-07-06 08:59 -0700
  Re: Does hashlib support a file mode? Peter Otten <__peter__@web.de> - 2011-07-06 18:26 +0200
    Re: Does hashlib support a file mode? Phlip <phlip2005@gmail.com> - 2011-07-06 09:49 -0700
      Re: Does hashlib support a file mode? Peter Otten <__peter__@web.de> - 2011-07-06 19:06 +0200
        Re: Does hashlib support a file mode? Phlip <phlip2005@gmail.com> - 2011-07-06 10:38 -0700
          Re: Does hashlib support a file mode? Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-06 13:42 -0500
      Re: Does hashlib support a file mode? Chris Torek <nospam@torek.net> - 2011-07-06 17:54 +0000

csiph-web