473,406 Members | 2,956 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,406 software developers and data experts.

Is anybody knows about a linkable, quick MD5/SHA1 calculator library ?

Hi !

I need to speedup my MD5/SHA1 calculator app that working on
filesystem's files.
I use the Python standard modules, but I think that it can be faster if
I use C, or other module for it.

I use FSUM before, but I got problems, because I "move" into "DOS area",
and the parameterizing of outer process maked me very angry (not working).
You will see this in this place:
http://mail.python.org/pipermail/pyt...ay/004697.html

So: I must handle unicode filenames. I think that if I find a library
that can working with py's unicode chars, and I can load and use it to
hash files, the code be better, and faster.

Anybody knows about same code ?

Py2.4, Windows, Py2Exe, wxPy... That was the specification.

Thanx for help:
dd
May 29 '06 #1
3 2680
On 30/05/2006 2:57 AM, DurumDara wrote:
Hi !

I need to speedup my MD5/SHA1 calculator app that working on
filesystem's files.
I use the Python standard modules, but I think that it can be faster if
I use C, or other module for it.

I use FSUM before, but I got problems, because I "move" into "DOS area",
and the parameterizing of outer process maked me very angry (not working).
You will see this in this place:
http://mail.python.org/pipermail/pyt...ay/004697.html

So: I must handle unicode filenames. I think that if I find a library
that can working with py's unicode chars, and I can load and use it to
hash files, the code be better, and faster.

Anybody knows about same code ?

Py2.4, Windows, Py2Exe, wxPy... That was the specification.


Hello (again), dd ...

As the effbot has said, the Python md5 and sha modules are written in C.
Hints: (1) the helpfile index says "builtin module" (2) you don't find a
sha.py or md5.py in c:\Python24\Lib\

An md5/sha library will concern itself with strings (which you obtain
from a file's *contents*), just like Python's modules do. Any struggle
with Unicode characters in the *names* of files is a separate concern.

Let's all stop worrying about low-level things like getting the 8.3
filename so that you can pass it to an MS-DOS program, and let's try to
explore why you think there is a problem with your initial approach.

At the end of this posting is a very simple Python function that
calculates the hash of a file (and its length), given the name of the
file (str or unicode, doesn't matter), which hashing module to use, and
a blocksize to use when reading. There is a really flash :-) user
interface that allows you to try it with either a glob pattern "*.txt",
or (as glob doesn't grok Windows mbcs/unicode filenames) a single
utf8-encoded filename.

Please try it out. My expectation is that, with a suitable choice of
blocksize, you will not be able to find anything that is significantly
faster and won't be difficult to interface to (like the FSUM program!).
If you have any problems or more questions, please don't hesitate to ask.

HTH,
John

=== function and driver ===
C:\junk>type hashtestbed.py

def hash_of_file(hash_module, fname, block_size):
f = open(fname, 'rb')
hashobj = hash_module.new()
filesize = 0
while True:
block = f.read(block_size)
if not block: break
filesize += len(block)
hashobj.update(block)
f.close()
return (filesize, hashobj.digest())

def to_hex(s):
return ''.join('%02x' % ord(c) for c in s)

if __name__ == "__main__":
import sha, md5, time, sys, glob
# print sys.argv
mdlname = sys.argv[1]
mdl = {'sha': sha, 'md5': md5}[mdlname]
szs = sys.argv[2].lower()
factor = {'m': 1024*1024, 'k': 1024}.get(szs[-1], 1)
if factor == 1:
bsz = int(szs)
else:
bsz = int(szs[:-1]) * factor
filearg = sys.argv[3]
if filearg.startswith("'"):
# repr(single filename, encoded in utf8)
filenames = [eval(filearg).decode('utf8')]
# print filenames
else:
filenames = glob.glob(sys.argv[3])
# I'm entering the above for the "Best UI of the Year" award :-)
for fn in filenames:
t0 = time.time()
fsz, digest = hash_of_file(mdl, fn, bsz)
seconds = time.time() - t0
print "%s, %r, bksz %d: %d bytes," \
" %.2f secs (%.4f secs/MB)\n\thash = %s" \
% (mdlname, fn, bsz, fsz,
seconds, seconds/fsz*1024*1024, to_hex(digest))

C:\junk>

=== sample usage ===

C:\junk>hashtestbed.py md5 32k '\xe5\xbc\xa0\xe6\x95\x8f.txt'
md5, u'\u5f20\u654f.txt', bksz 32768: 17 bytes, 0.00 secs (0.0000 secs/MB)
hash = 746d0931605368989a20691a906a67f8

C:\junk>hashtestbed.py md5 32k \downloads\python*.msi
md5, '\\downloads\\python-2.4.2.msi', bksz 32768: 9671168 bytes, 0.08
secs (0.00
86 secs/MB)
hash = bfb6fc0704d225c7a86d4ba8c922c7f5
md5, '\\downloads\\python-2.4.3.msi', bksz 32768: 9688576 bytes, 0.06
secs (0.00
67 secs/MB)
hash = ab946459d7cfba4a8500f9ff8d35cc97
md5, '\\downloads\\python-2.5a2.msi', bksz 32768: 10274816 bytes, 0.05
secs (0.0
048 secs/MB)
hash = cedc1e1fed9c4cd137921a80485bf007

=== end ===
May 30 '06 #2
DurumDara wrote:
Hi !

I need to speedup my MD5/SHA1 calculator app that working on
filesystem's files.


You could try using threads. This would allow the CPU and the disk to
work in parallel.

The sha/md5 modules don't seem to release the global interpreter lock,
so you won't be able to use multiple CPUs/cores yet.

Daniel
May 30 '06 #3
DurumDara wrote:
Hi !

I need to speedup my MD5/SHA1 calculator app that working on
filesystem's files.
I use the Python standard modules, but I think that it can be faster if
I use C, or other module for it.

I use FSUM before, but I got problems, because I "move" into "DOS area",
and the parameterizing of outer process maked me very angry (not working).
You will see this in this place:
http://mail.python.org/pipermail/pyt...ay/004697.html


FWIW I looked at what is the problem, apparently fsum converts the name
back to unicode, tries to print it and silently corrupts the output.
You give it short name XA02BB~1 of the file xA˙ and fsum prints xA

Use python module or try another utility.

May 30 '06 #4

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

Similar topics

3
by: Saļd | last post by:
Hi, Is there a pure C library that emulate a calculator (with +-/* operations, plus log, sin,cos... functions). I would like to call such a program in a way resembling this: ...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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.