470,815 Members | 1,272 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

md5 from python different then md5 from command line

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

May 7 '06 #1
9 1713
In article <11**********************@j73g2000cwa.googlegroups .com>,
"ur************@gmail.com" <ur************@gmail.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
May 7 '06 #2
ur************@gmail.com 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
May 7 '06 #3
> echo adds a newline:
>>> import md5
>>> test = md5.new("marius\n")
>>> print test.hexdigest()

0f0f60ac801a9eec2163083a22307deb

Just

Thanks, that was it ;)

May 7 '06 #4
"Marius Ursache" <ur************@gmail.com> 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 -
May 7 '06 #5
ur************@gmail.com 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.
May 7 '06 #6
John Salerno <jo******@NOSPAMgmail.com> 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()
May 7 '06 #7
Paul Rubin wrote:
John Salerno <jo******@NOSPAMgmail.com> 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.
May 7 '06 #8
John Salerno <jo******@NOSPAMgmail.com> 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
May 7 '06 #9
Paul Rubin wrote:
John Salerno <jo******@NOSPAMgmail.com> 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. :)
May 8 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by jtan325 | last post: by
8 posts views Thread by Paul Cochrane | last post: by
12 posts views Thread by adamurbas | last post: by
48 posts views Thread by Yves Dorfsman | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.