473,573 Members | 3,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question regarding checksuming of a file

Good evening,

I need to generate checksums of a file, store the value in a variable,
and pass it along for later comparison.

The MD5 module would seem to do the trick but I'm sketchy on implementation.
The nearest I can see would be

import md5

m=md5.new()
contents = open(self.file_ name,"rb").read ()
check=md5.updat e(contents)

However this does not appear to be actually returning the checksum.

Does anyone have insight into where I am going wrong?

Any help you can provide would be greatly appreciated.

Thanks
May 14 '06 #1
10 1377
Andrew Robert wrote:
m=md5.new()
contents = open(self.file_ name,"rb").read ()
check=md5.updat e(contents)

However this does not appear to be actually returning the checksum.


the docs are your friend, use them. hint: first you eat, then you...
http://docs.python.org/lib/module-md5.html

--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
May 14 '06 #2
Actually, I think I got it but would like to confirm this looks right.

import md5
checksum = md5.new()
mfn = open(self.file_ name, 'r')
for line in mfn.readlines() :
checksum.update (line)
mfn.close()
cs = checksum.hexdig est()
print cs

The value cs should contain the MD5 checksum or did I miss something?

Any help you can provide would be greatly appreciated.

Thanks
May 14 '06 #3
In article <12************ *@corp.supernew s.com>,
Andrew Robert <an************ @gmail.com> wrote:
Good evening,

I need to generate checksums of a file, store the value in a variable,
and pass it along for later comparison.

The MD5 module would seem to do the trick but I'm sketchy on implementation.
The nearest I can see would be

import md5

m=md5.new()
contents = open(self.file_ name,"rb").read ()
check=md5.updat e(contents)

However this does not appear to be actually returning the checksum.

Does anyone have insight into where I am going wrong?


After calling update(), you need to call digest(). Update() only updates
the internal state of the md5 state machine; digest() returns the hash.
Also, for the code above, it's m.update(), not md5.update(). Update() is a
method of an md5 instance object, not the md5 module itself.

Lastly, the md5 algorithm is known to be weak. If you're doing md5 to
maintain compatability with some pre-existing implementation, that's one
thing. But, if you're starting something new from scratch, I would suggest
using SHA-1 instead (see the sha module). SHA-1 is much stronger
cryptographical ly than md5. The Python API is virtually identical, so it's
no added work to switch to the stronger algorithm.
May 14 '06 #4
Roy Smith wrote:

However this does not appear to be actually returning the checksum.

Does anyone have insight into where I am going wrong?


After calling update(), you need to call digest(). Update() only updates
the internal state of the md5 state machine; digest() returns the hash.
Also, for the code above, it's m.update(), not md5.update(). Update() is a
method of an md5 instance object, not the md5 module itself.

Lastly, the md5 algorithm is known to be weak. If you're doing md5 to
maintain compatability with some pre-existing implementation, that's one
thing. But, if you're starting something new from scratch, I would suggest
using SHA-1 instead (see the sha module). SHA-1 is much stronger
cryptographical ly than md5. The Python API is virtually identical, so it's
no added work to switch to the stronger algorithm.


Hi Roy,

This is strictly for checking if a file was corrupted during transit
over an MQSeries channel.

The check is not intended to be used for crypto purposes.
May 14 '06 #5
Ant
A script I use for comparing files by MD5 sum uses the following
function, which you may find helps:

def getSum(self):
md5Sum = md5.new()

f = open(self.filen ame, 'rb')

for line in f:
md5Sum.update(l ine)

f.close()

return md5Sum.hexdiges t()

May 14 '06 #6
"Ant" <an****@gmail.c om> writes:
def getSum(self):
md5Sum = md5.new()
f = open(self.filen ame, 'rb')
for line in f:
md5Sum.update(l ine)
f.close()
return md5Sum.hexdiges t()


This should work, but there is one hazard if the file is very large
and is not a text file. You're trying to read one line at a time from
it, which means a contiguous string of characters up to a newline.
Depending on the file contents, that could mean gigabytes which get
read into memory. So it's best to read a fixed size amount in each
operation, e.g. (untested):

def getblocks(f, blocksize=1024) :
while True:
s = f.read(blocksiz e)
if not s: return
yield s

then change "for line in f" to "for line in f.getblocks()".

I actually think an iterator like the above should be added to the
stdlib, since the "for line in f" idiom is widely used and sometimes
inadvisable, like the fixed sized buffers in those old C programs
that led to buffer overflow bugs.
May 14 '06 #7

When I run the script, I get an error that the file object does not have
the attribute getblocks.

Did you mean this instead?

def getblocks(f, blocksize=1024) :
while True:
s = f.read(blocksiz e)
if not s: return
yield s

def getsum(self):
md5sum = md5.new()
f = open(self.file_ name, 'rb')
for line in getblocks(f) :
md5sum.update(l ine)
f.close()
return md5sum.hexdiges t()

May 14 '06 #8
Am Sonntag 14 Mai 2006 20:51 schrieb Andrew Robert:
def getblocks(f, blocksize=1024) :
while True:
s = f.read(blocksiz e)
if not s: return
yield s


This won't work. The following will:

def getblocks(f,blo cksize=1024):
while True:
s = f.read(blocksiz e)
if not s: break
yield s

--- Heiko.
May 14 '06 #9
Andrew Robert <an************ @gmail.com> writes:
When I run the script, I get an error that the file object does not have
the attribute getblocks.


Woops, yes, you have to call getblocks(f). Also, Heiko says you can't
use "return" to break out of the generator; I thought you could but
maybe I got confused.
May 14 '06 #10

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

Similar topics

1
3253
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data structures (ints), and more complex data structures (strings and vectors) in it, and would like to write the entire class to a data file that could then...
18
7325
by: Andre Laplume via AccessMonster.com | last post by:
I have inherited a bunch of dbs which are are shared among a small group in my dept. We typically use the dbs to write queries to extract data, usually dumping it into Excel. Most dbs originated in MsAccess 97 or prior and have been converted to 2003. On occassion user 1 will open a db. When user 2 opens the db it will not let user 2...
10
3429
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read somewhere that each folder under the "web site" is compiled in separate assembly. I however, did not find that the "web site" creation in vs.net...
6
1506
by: Jon | last post by:
All, I'm working in a fairly robust content management system for our company's websites, and have a question regarding the file and directory structure of the site. Currently, I'm populating all content from each page using data in a MySQL DB, and php to call the DB and push out the info. Fairly standard right? Well, my question lies in...
1
1332
by: Terrance | last post by:
I was wondering if someone can help me with my question regarding the configuration system functionality in the .NET Framework 2.0 for VB. My question is, if I have a application configuration file; can the values in the file be changed? I read that the Application settings are read-only; is there any way of changing the data programmatically?...
42
6764
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first." ...
1
1234
by: archana | last post by:
Hi all, I am new to asp.net. I have one question regarding code- behind model. I have written page_load event in code behind as well as in aspx page. Means i am doing mixing of code-behind as well as single file model. Is it possible to write some code in code behind and some in aspx page itself.
1
1545
by: Nathan | last post by:
Hello, I have a programming assignment due for my programming class. Basically, I have to code the program in visual studio..then I have to paste the code and the output into microsoft word(2007). Well my friend told me that you can copy and paste the "outputbox" into Word. When i say "output box"..i mean the display window that pops up...
54
3567
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header file writing: const double PI = 3.14; Every time using it, include the header file.
0
7778
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8017
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8201
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7777
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3731
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1300
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1039
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.