Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > md5 from python different then md5 from command line

Reply
Thread Tools

md5 from python different then md5 from command line

 
 
ursache.marius@gmail.com
Guest
Posts: n/a
 
      05-07-2006
Hi

I noticed that the md5 computed with md5 module from python is
different then the md5 sum computed with md5sum utility (on slackware
and gentoo).

i.e.
$echo marius|md5sum
0f0f60ac801a9eec2163083a22307deb -

>>> test = md5.new("marius")
>>> print test.hexdigest()

242aa1a97769109065e3b4df359bcfc9


Any idea why? and how to get the same md5 sum for both calls?

Thanks

 
Reply With Quote
 
 
 
 
Dejan Rodiger
Guest
Posts: n/a
 
      05-07-2006
said the following on 07.05.2006 12:07:
> Hi
>
> I noticed that the md5 computed with md5 module from python is
> different then the md5 sum computed with md5sum utility (on slackware
> and gentoo).
>
> i.e.
> $echo marius|md5sum
> 0f0f60ac801a9eec2163083a22307deb -
>
>>>> test = md5.new("marius")
>>>> print test.hexdigest()

> 242aa1a97769109065e3b4df359bcfc9
>
>
> Any idea why? and how to get the same md5 sum for both calls?
>
> Thanks
>


try md5sum marius
probably "new line" character is not computed in "echo marius|md5sum"


--
Dejan Rodiger - PGP ID 0xAC8722DC
Delete wirus from e-mail address
 
Reply With Quote
 
 
 
 
Just
Guest
Posts: n/a
 
      05-07-2006
In article < .com>,
"" <> wrote:

> I noticed that the md5 computed with md5 module from python is
> different then the md5 sum computed with md5sum utility (on slackware
> and gentoo).
>
> i.e.
> $echo marius|md5sum
> 0f0f60ac801a9eec2163083a22307deb -
>
> >>> test = md5.new("marius")
> >>> print test.hexdigest()

> 242aa1a97769109065e3b4df359bcfc9
>
>
> Any idea why? and how to get the same md5 sum for both calls?


echo adds a newline:

>>> import md5
>>> test = md5.new("marius\n")
>>> print test.hexdigest()

0f0f60ac801a9eec2163083a22307deb

Just
 
Reply With Quote
 
Marius Ursache
Guest
Posts: n/a
 
      05-07-2006
> echo adds a newline:
>
> >>> import md5
> >>> test = md5.new("marius\n")
> >>> print test.hexdigest()

> 0f0f60ac801a9eec2163083a22307deb
>
> Just



Thanks, that was it

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      05-07-2006
"Marius Ursache" <> writes:
> > >>> import md5
> > >>> test = md5.new("marius\n")
> > >>> print test.hexdigest()

> > 0f0f60ac801a9eec2163083a22307deb

>
> Thanks, that was it


Also, the -n option suppresses the newline from echo:

$ echo -n marius | md5sum
242aa1a97769109065e3b4df359bcfc9 -
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      05-07-2006
wrote:
> Hi
>
> I noticed that the md5 computed with md5 module from python is
> different then the md5 sum computed with md5sum utility (on slackware
> and gentoo).
>
> i.e.
> $echo marius|md5sum
> 0f0f60ac801a9eec2163083a22307deb -
>
>>>> test = md5.new("marius")
>>>> print test.hexdigest()

> 242aa1a97769109065e3b4df359bcfc9
>
>
> Any idea why? and how to get the same md5 sum for both calls?
>
> Thanks
>


Just a quick md5-related follow-up question: I was experimenting with it
and making md5 sums for strings, but how do you use the md5 module to
create a sum for an actual file, such as an .exe file?

Thanks.
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      05-07-2006
John Salerno <> writes:
> Just a quick md5-related follow-up question: I was experimenting with
> it and making md5 sums for strings, but how do you use the md5 module
> to create a sum for an actual file, such as an .exe file?


m = md5.new()
f = file('foo.exe', 'b') # open in binary mode
while True:
t = f.read(1024)
if len(t) == 0: break # end of file
m.update(t)
print m.hexdigest()
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      05-07-2006
Paul Rubin wrote:
> John Salerno <> writes:
>> Just a quick md5-related follow-up question: I was experimenting with
>> it and making md5 sums for strings, but how do you use the md5 module
>> to create a sum for an actual file, such as an .exe file?

>
> m = md5.new()
> f = file('foo.exe', 'b') # open in binary mode
> while True:
> t = f.read(1024)
> if len(t) == 0: break # end of file
> m.update(t)
> print m.hexdigest()


Any reason you can't just read the whole file at once and update m?

Also, doesn't the parameter for update have to be a string? If you're
reading the file in binary mode, would t still be a string?

Thanks.
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      05-07-2006
John Salerno <> writes:
> Any reason you can't just read the whole file at once and update m?


Yes, you could say

print md5.new(file('foo.exe').read()).hexdigest()


but that means reading the whole file into memory at once. If the
file is very large, that could thrash or fail.

> Also, doesn't the parameter for update have to be a string? If you're
> reading the file in binary mode, would t still be a string?


Yes, t would still be a string. You can have NUL bytes and so forth
in Python strings:

len('ab\0cd') ==> 5
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      05-07-2006
Paul Rubin wrote:
> John Salerno <> writes:
>> Any reason you can't just read the whole file at once and update m?

>
> Yes, you could say
>
> print md5.new(file('foo.exe').read()).hexdigest()
>
>
> but that means reading the whole file into memory at once. If the
> file is very large, that could thrash or fail.
>
>> Also, doesn't the parameter for update have to be a string? If you're
>> reading the file in binary mode, would t still be a string?

>
> Yes, t would still be a string. You can have NUL bytes and so forth
> in Python strings:
>
> len('ab\0cd') ==> 5


Thanks! I didn't expect it to be so easy.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting command line in python MrSmile Python 5 11-09-2011 09:20 AM
python from any command line? waltbrad Python 5 12-09-2007 01:21 PM
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM
Python from the command line (was: Choosing Perl/Python for my particular niche) Cameron Laird Python 3 04-01-2004 10:21 PM
Python from the command line (was: Choosing Perl/Python for my particular niche) Cameron Laird Perl Misc 2 04-01-2004 12:50 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57