473,378 Members | 1,421 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,378 software developers and data experts.

Help getting the md5 module to work

I'm trying to use the md5 module but it gives me a different result from
what other programs give. This is the code I have and the result from
hexdigest() never matches what I expect. Its probably something stupid
but its driving me crazy.

f = file (filename, "r")
m = md5.new()
data = f.read (CHUNK_SIZE)
while data != "":
m.update (data)
data = f.read (CHUNK_SIZE)
Jul 18 '05 #1
7 1317
Grumfish wrote:
I'm trying to use the md5 module but it gives me a different result from
what other programs give. This is the code I have and the result from
hexdigest() never matches what I expect. Its probably something stupid
but its driving me crazy.

f = file (filename, "r")
m = md5.new()
data = f.read (CHUNK_SIZE)
while data != "":
m.update (data)
data = f.read (CHUNK_SIZE)

I'll wager this is happening on Windows. When reading data in text
mode on Windows, Python converts "\r\n" sequence to "\n". You need to
open the file in binary mode. Try changing the first line to:

f = file(filename,"rb")


--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
Jul 18 '05 #2
I have never used the md5 module that python offers... but after reading
this post I thought I would try it out.
n = md5.new()
n.update("help")
n.hexdigest()
'657f8b8da628ef83cf69101b6817150a'

But I run FreeBSD 5.0 and when I execute
$ echo help | openssl md5
45b758a4f518f3ff31363696132f5f5a

What gives? What am I missing here?

Thanks in advance for shedding some light on my "easy" question.
Carl Banks" <im*****@aerojockey.invalid> wrote in message
news:m1*******************@fe1.columbus.rr.com... Grumfish wrote:
I'm trying to use the md5 module but it gives me a different result from
what other programs give. This is the code I have and the result from
hexdigest() never matches what I expect. Its probably something stupid
but its driving me crazy.

f = file (filename, "r")
m = md5.new()
data = f.read (CHUNK_SIZE)
while data != "":
m.update (data)
data = f.read (CHUNK_SIZE)

I'll wager this is happening on Windows. When reading data in text
mode on Windows, Python converts "\r\n" sequence to "\n". You need to
open the file in binary mode. Try changing the first line to:

f = file(filename,"rb")


--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon

Jul 18 '05 #3
"Amy G" <am*******@cox.net> writes:
n.update("help")
n.hexdigest()

'657f8b8da628ef83cf69101b6817150a'

But I run FreeBSD 5.0 and when I execute
$ echo help | openssl md5
45b758a4f518f3ff31363696132f5f5a

What gives? What am I missing here?

Thanks in advance for shedding some light on my "easy" question.


Try n.update("help\n").
Jul 18 '05 #4
Paul Rubin wrote:
Try n.update("help\n").


Right. For Amy: You can suppress the newline on echo with the -n
option, by the way:

max@oxygen:~% echo -n help | md5sum
657f8b8da628ef83cf69101b6817150a -

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ We are circumstances of our birth.
-- Sade Adu
Jul 18 '05 #5

"Erik Max Francis" <ma*@alcyone.com> wrote in message
news:40***************@alcyone.com...
Paul Rubin wrote:
Try n.update("help\n").


Right. For Amy: You can suppress the newline on echo with the -n
option, by the way:

I discovered this last night.

But what is the true MD5 hash for help? With or without the newline?

Jul 18 '05 #6
Amy G wrote:
"Erik Max Francis" <ma*@alcyone.com> wrote in message
news:40***************@alcyone.com...
Paul Rubin wrote:

Try n.update("help\n").


Right. For Amy: You can suppress the newline on echo with the -n
option, by the way:


I discovered this last night.

But what is the true MD5 hash for help? With or without the newline?


You already had the right hashes for both! Your Python attempt did
not include a newline, while the shell approach with "echo" did, so you
were comparing apples and oranges.

Paul and Erik gave you the solutions to switch either approach to do
what the other does...

-Peter
Jul 18 '05 #7
Amy G wrote:
I discovered this last night.

But what is the true MD5 hash for help? With or without the newline?


It's completely up to you. It's your hash, you decide what goes in it.
All that's important is whoever's comparing hashes with yours (for
whatever reason) is doing it consistently.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ I'm spreading my wings and I fly / I'm still as the night
-- Chante Moore
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marc | last post by:
Hello, I've fiddled with this for quite a while and thought I had the problem solved. I had a version that would successfully compile and run. But then I had to change the code to use a...
2
by: WAYNEL | last post by:
Hi I am trying to re-write some of the example code that Agilent gives for VB to VB.Net. In .Net I keep getting the error 'cannot change the number of dimensions of an array'. I have paste...
8
by: Dalan | last post by:
Please help - just take a quick look at the function code below. It probably just needs some minor tweaking. The function module is based on an intermediate query to provide a group record...
4
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual...
6
by: Jax | last post by:
I have Visual Studio 2002 Standard Edition. It has been working fine up to a point and now i'm at that point. Due to the limitations of the edition i am not using any of my own .dll's and instead...
2
by: Steve K | last post by:
I got a bit of a problem I like some help on. I'm designing an online training module for people that work in food processing plants. This is my target audience. These workers have little or no...
4
by: Vlad Dogaru | last post by:
Hello, I am trying to use cookies and Python to create a simple login example. But I am very disoriented at the existence of two cookie libraries, namely Cookie and cookielib. I have seen...
3
by: Steve Lowe | last post by:
Hi, I'm getting into VB.net with the help of a few books, but have got a problem grasping how to implement a system.timers.timer Only 1 of my 3 books mentions timers and that's a...
0
by: Mitko Haralanov | last post by:
I am attempting to get a working wrapper around the TransactionSet class from the rpm module but I am getting unexplained segfaults in /usr/bin/python. The reason why I am working on a wrapper...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.