[IronPython] hashlib for IronPython

Sanghyeon Seo sanxiyn at gmail.com
Wed Oct 11 10:36:54 CEST 2006


hashlib is a new module using C extension added in CPython 2.5.
http://docs.python.org/lib/module-hashlib.html

This is now implemented in FePy:
https://svn.sourceforge.net/svnroot/fepy/trunk/lib/hashlib.py

In CPython 2.5, md5 and sha modules are implemented as a thin wrapper
around this module. The same is true for FePy now:
https://svn.sourceforge.net/svnroot/fepy/trunk/lib/md5.py
https://svn.sourceforge.net/svnroot/fepy/trunk/lib/sha.py

You can read the source, but here are some technical details:

In CPython, hashlib is backed by OpenSSL. hashlib.new can create hash
object by name, in contrast to static constructors like hashlib.md5.
This is implemented by OpenSSL function EVP_get_digestbyname.

I have mapped this to HashAlgorithm.Create, which was an obvious
choice. This can be used, for example, to load RIPEMD160 which doesn't
have static constructor in hashlib.

Oh, by the way, I updated hash_test.py too, and you can see the usage
there. hashlib import is conditionalized to make it run on CPython 2.4
too.
https://svn.sourceforge.net/svnroot/fepy/trunk/example/hash_test.py

There were two problems:

1. .NET framework doesn't provide SHA224 hash algorithm, but it is
always present in CPython hashlib. I raise NotImplementedError for
this case.

2. Mono's HashAlgorithm.Create doesn't accept lowercased algorithm
names, while both MS.NET and CPython do. I convert the name to the
uppercase myself.

There's also more difficult problem which I will write about in the next post.

-- 
Seo Sanghyeon



More information about the Ironpython-users mailing list