[PYTHON-CRYPTO] Hashing modules

Andrew Kuchling akuchlin at CNRI.RESTON.VA.US
Thu Feb 8 22:41:10 CET 2001


Let's take a look at the interface for hashing.  Here are the docs
from amkCrypto:

==========================

   All hashing modules share the same interface. After importing a given
   hashing module, call the new() function to create a new hashing
   object. (In older versions of the Python interpreter, the
   md5() function was used to perform this task. If you're modifying an
   old script, you should change any calls to md5() to use new()
   instead.) You can now feed arbitrary strings into the object, and can
   ask for the hash value at any time. The new() function can also be
   passed an optional string parameter, which will be hashed immediately.

   Hash function modules define one variable:

   digestsize
          An integer value; the size of the digest produced by the
          hashing objects. You could also obtain this value by creating a
          sample object, and taking the length of the digest string it
          returns, but using digestsize is faster.

   The methods for hashing objects are always the following:

   copy ()
          Return a separate copy of this hashing object. An update to
          this copy won't affect the original object.

   digest ()
          Return the hash value of this hashing object, as a string
          containing 8-bit data. The object is not altered in any way by
          this function; you can continue updating the object after
          calling this function.

   hexdigest ()
          Return the hash value of this hashing object, as a string
          containing the digest data as hexadecimal digits. The resulting
          string will be twice as long as that returned by digest(). The
          object is not altered in any way by this function; you can
          continue updating the object after calling this function.

   update (arg)
          Update this hashing object with the string arg.

   Here's an example, using RSA Data Security's MD5 algorithm:

>>> from Crypto.Hash import MD5
>>> m = MD5.new()
>>> m.update('abc')
>>> m.digest()
'\220\001P\230<\322O\260\326\226?@}(\341\177r'

   Or, more compactly:

>>> MD5.new('abc').digest()
'\220\001P\230<\322O\260\326\226?@}(\341\177r'

===========================================================================

Does this seem a reasonable interface for a hash algorithm?  Anything
missing?  Anything that can be dropped?  (.hexdigest() probably isn't
needed... or is it?)

Any ideas/features from other languages that we can steal?  Java's API
is documented at
http://java.sun.com/j2se/1.3/docs/api/java/security/MessageDigest.html;
can anyone find the docs for the Perl modules?

--amk





More information about the python-crypto mailing list