473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 1766
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: calmar | last post by:
Hi all, I would like to use python for a replacement for some binutils. I would like to be able to pipe things into python. Actually I would not like writing a 'script' to handle the input, but...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
1
by: jtan325 | last post by:
hi, i am running Linux Ubuntu Hoary and am trying to build the Python numarray package, v. 1.3.2 by hand since ubuntu's repos won't be updated until breezy. i have python 2.4, and gcc 3.3.5 ...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
2
by: Pierre Rouleau | last post by:
Hi all, I have a consistent test case where os.popen3() hangs in Windows. The system hangs when retrieving the lines from the child process stdout. I know there were several reports related to...
34
by: Ben Sizer | last post by:
I've installed several different versions of Python across several different versions of MS Windows, and not a single time was the Python directory or the Scripts subdirectory added to the PATH...
9
by: PengYu.UT | last post by:
Hi, I feel argparse has some useful things that optparse doesn't have. But I can't find it argparse in python library reference. I'm wondering when it will be available in the python standard...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
48
by: Yves Dorfsman | last post by:
On UNIX, some people use #!/usr/bin/env python While other use #!/usr/bin/python Why is one preferred over the other one ? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.