python /usr/local/lib/python/demo/md5test/md5driver.py -x
I looked inside, but didn't see any obvious explanation for why it didn't do
anything --- understandable, since I am just getting started with Python and
haven't even finished digesting the reference manual yet. Then I've got to
read the library reference, and put a tuck in the airedale.
So anyway, I wanted to make sure I had built a Python whose MD5 support
worked --- it seemed like a moderately amusing first program in Python.
Here's what I ended up with:
#!/usr/local/bin/python
from md5 import md5
def hexstr(s):
r = ''
for c in s:
r = r + ('0' + hex(ord(c))[2:])[-2:]
return r
def md5test(s):
return 'MD5 ("' + s + '") = ' + hexstr(md5(s).digest())
print 'MD5 test suite:'
print md5test('')
print md5test('a')
print md5test('abc')
print md5test('message digest')
print md5test('abcdefghijklmnopqrstuvwxyz')
print md5test('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
print md5test('12345678901234567890123456789012345678901234567890123456789012345678901234567890')
Now I'll admit that might not be the prettiest solution, but it's what came
into my head, and it did produce output matching the test suite from
md5.txt. So next I checked to see how md5driver.py did the hex string
printing; it was much more graceful. So I redid my hexstr subroutine and it
still worked:
#!/usr/local/bin/python
from md5 import md5
import string
h = string.hexdigits
def hexstr(s):
r = ''
for c in s:
i = ord(c)
r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
return r
def md5test(s):
return 'MD5 ("' + s + '") = ' + hexstr(md5(s).digest())
print 'MD5 test suite:'
print md5test('')
print md5test('a')
print md5test('abc')
print md5test('message digest')
print md5test('abcdefghijklmnopqrstuvwxyz')
print md5test('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
print md5test('12345678901234567890123456789012345678901234567890123456789012345678901234567890')
So, anybody want to help a Python newbie with tips about how this and that
could be done more cleanly, beautifully, efficiently, etc.?
-Bennett
bet@sbi.com