2001-03-27 12:42:59 -05:00
|
|
|
|
PEP: 247
|
|
|
|
|
Title: API for Cryptographic Hash Functions
|
|
|
|
|
Version: $Revision$
|
2001-07-08 23:57:09 -04:00
|
|
|
|
Author: A.M. Kuchling <akuchlin@mems-exchange.org>
|
2001-03-28 15:18:03 -05:00
|
|
|
|
Status: Draft
|
2001-03-27 12:42:59 -05:00
|
|
|
|
Type: Informational
|
|
|
|
|
Created: 23-Mar-2001
|
|
|
|
|
Post-History:
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
There are several different modules available that implement
|
|
|
|
|
cryptographic hashing algorithms such as MD5 or SHA. This
|
|
|
|
|
document specifies a standard API for such algorithms, to make it
|
|
|
|
|
easier to switch between different implementations.
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Specification
|
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
All hashing modules should present the same interface. Additional
|
|
|
|
|
methods or variables can be added, but those described in this
|
|
|
|
|
document should always be present.
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
Hash function modules define one function:
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
new([string], [key])
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
Create a new hashing object and return it. You can now feed
|
|
|
|
|
arbitrary strings into the object using its update() method,
|
|
|
|
|
and can ask for the hash value at any time by calling its
|
|
|
|
|
digest() method.
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
The 'string' parameter, if supplied, will be immediately hashed
|
|
|
|
|
into the object's starting state. For keyed hashes such as
|
|
|
|
|
HMAC, 'key' is a string containing the starting key.
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
Hash function modules define one variable:
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
digest_size
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
An integer value; the size of the digest produced by the
|
|
|
|
|
hashing objects, measured in bytes. You could also obtain
|
|
|
|
|
this value by creating a sample object, and taking the length
|
|
|
|
|
of the digest string it returns, but using digest_size is
|
|
|
|
|
faster.
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
The methods for hashing objects are always the following:
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
clear()
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
Reset this hashing object to its initial state, as if the
|
|
|
|
|
object was created from new(key=<initially specified key>)
|
|
|
|
|
(XXX what should this do for a keyed hash? A proposed
|
|
|
|
|
semantic follows:). There is no way to supply a starting
|
|
|
|
|
string as there is for the new() function, and there's no way
|
|
|
|
|
to change the key specified for a keyed hash.
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
copy()
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
Return a separate copy of this hashing object. An update to
|
|
|
|
|
this copy won't affect the original object.
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
digest()
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
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.
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
update(arg)
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
Update this hashing object with the string arg.
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
For convenience, hashing objects also have a digest_size
|
|
|
|
|
attribute, measuring the size of the resulting digest in bytes.
|
|
|
|
|
This is identical to the module-level digest_size variable.
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
Here's an example, using a module named 'MD5':
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
>>> from Crypto.Hash import MD5
|
|
|
|
|
>>> m = MD5.new()
|
|
|
|
|
>>> m.update('abc')
|
|
|
|
|
>>> m.digest()
|
|
|
|
|
'\220\001P\230<\322O\260\326\226?@}(\341\177r'
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
2001-03-28 15:18:03 -05:00
|
|
|
|
Or, more compactly:
|
|
|
|
|
|
|
|
|
|
>>> MD5.new('abc').digest()
|
|
|
|
|
'\220\001P\230<\322O\260\326\226?@}(\341\177r'
|
2001-03-27 12:42:59 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
End:
|
|
|
|
|
|