Re: wired md5 hashing problem
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: wired md5 hashing problem

From: Adam DePrince <adam.deprince@gmail.com>
Date: Sun Mar 26 2006 - 18:57:56 CEST

On Sun, 2006-03-26 at 16:56 +0200, Matthias Güntert wrote:
> Hello list-members
>
> i am in the process of writing a python script to backup my data. Now I
> would like to implement md5/sha1 hashes.
>
> # do md5 fingerprinting
> if config.get("global", "crc") == "md5":
> m = md5.new()
> # open the file
> f = open(fname, "rb")
> while 1:
> block = f.read(1024*1024)
> if not block:
> break
> # generate the hash
> m.update(block)
> f.close()
>
> # write the results properly formated to a file
> fd = file(fname + ".md5", "w")
> fd.write(m.hexdigest())
> fd.write(" " + fname + "\n")
> fd.close()
>
>
> mguentert@uranos > md5sum -c backup.tar.bz2.md5
> /fileservice/temp/backup.tar.bz2: FAILED
> md5sum: WARNING: 1 of 1 computed checksum did NOT match
>
> mguentert@uranos > cat backup.tar.bz2.md5
> d41d8cd98f00b204e9800998ecf8427e /fileservice/temp/backup.tar.bz2

Hey, I found an md5 collision for your file!

>>> import md5
>>> md5.new().hexdigest()
'd41d8cd98f00b204e9800998ecf8427e'
>>>
[adam@localhost Include]$ md5sum # hit ^d at start
d41d8cd98f00b204e9800998ecf8427e -

Your file was empty when scanned.

Without more information, I'd say that your file was empty when you ran
your python code.

But your code does work ...

import md5
m = md5.new()
# open the file
fname="Python-2.4.2.tar.bz2"
f = open(fname, "rb" )
while 1:
    block = f.read(1024*1024)
    if not block:
        break
    # generate the hash
    m.update(block)
f.close()
fd = file(fname + ".md5", "w")
fd.write(m.hexdigest())
fd.write(" " + fname + "\n")
fd.close()

[adam@localhost ~]$ python test2.py
[adam@localhost ~]$ md5sum -c Python-2.4.2.tar.bz2.md5
Python-2.4.2.tar.bz2: OK

- Adam
Received on Sun Apr 30 21:14:02 2006