md5 from python different then md5 from command line

U

ursache.marius

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 -
242aa1a97769109065e3b4df359bcfc9


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

Thanks
 
D

Dejan Rodiger

(e-mail address removed) 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 -

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"
 
J

Just

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 -

242aa1a97769109065e3b4df359bcfc9


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

echo adds a newline:
0f0f60ac801a9eec2163083a22307deb

Just
 
P

Paul Rubin

Marius Ursache said:
Thanks, that was it ;)

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

$ echo -n marius | md5sum
242aa1a97769109065e3b4df359bcfc9 -
 
J

John Salerno

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 -

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.
 
P

Paul Rubin

John Salerno said:
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()
 
J

John Salerno

Paul said:
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.
 
P

Paul Rubin

John Salerno said:
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
 
J

John Salerno

Paul said:
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.


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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top