Apply suggestions from last round of comments
This commit is contained in:
parent
0f209d79a3
commit
058fd0c7cc
86
pep-0247.txt
86
pep-0247.txt
|
@ -30,31 +30,42 @@ Specification
|
||||||
and can ask for the hash value at any time by calling its
|
and can ask for the hash value at any time by calling its
|
||||||
digest() method.
|
digest() method.
|
||||||
|
|
||||||
The 'string' parameter, if supplied, will be immediately hashed
|
The 'string' parameter, if supplied, will be immediately
|
||||||
into the object's starting state. For keyed hashes such as
|
hashed into the object's starting state; an empty string or
|
||||||
HMAC, 'key' is a string containing the starting key.
|
None. For keyed hashes such as HMAC, 'key' is a string
|
||||||
|
containing the starting key.
|
||||||
|
|
||||||
|
Arbitrary additional keyword arguments can be added to this
|
||||||
|
function, but if they're not supplied, sensible default values
|
||||||
|
should be used. For example, 'rounds' and 'digestsize' could
|
||||||
|
be added for a hash function which supports a different output
|
||||||
|
size and a different number of rounds.
|
||||||
|
|
||||||
Hash function modules define one variable:
|
Hash function modules define one variable:
|
||||||
|
|
||||||
digest_size
|
digestsize
|
||||||
|
|
||||||
An integer value; the size of the digest produced by the
|
An integer value; the size of the digest produced by the
|
||||||
hashing objects, measured in bytes. You could also obtain
|
hashing objects created by this module, measured in bytes.
|
||||||
this value by creating a sample object, and taking the length
|
You could also obtain this value by creating a sample object
|
||||||
of the digest string it returns, but using digest_size is
|
and accessing its 'digestsize' attribute, but it can be
|
||||||
faster.
|
convenient to have this value available from the module.
|
||||||
|
Hashes with a variable output size will set this variable to 0.
|
||||||
|
|
||||||
|
The attributes of a hashing object are:
|
||||||
|
|
||||||
|
digestsize
|
||||||
|
|
||||||
|
An integer value; the size of the digest produced by the
|
||||||
|
hashing object, measured in bytes. If the hash has a variable
|
||||||
|
output size, this output size must be chosen when the
|
||||||
|
hashing object is created, and this attribute must contain the
|
||||||
|
selected size. Therefore 0 is *not* a legal value for this
|
||||||
|
attribute.
|
||||||
|
|
||||||
|
|
||||||
The methods for hashing objects are always the following:
|
The methods for hashing objects are always the following:
|
||||||
|
|
||||||
clear()
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
copy()
|
copy()
|
||||||
|
|
||||||
Return a separate copy of this hashing object. An update to
|
Return a separate copy of this hashing object. An update to
|
||||||
|
@ -62,14 +73,30 @@ Specification
|
||||||
|
|
||||||
digest()
|
digest()
|
||||||
|
|
||||||
Return the hash value of this hashing object, as a string
|
Return the hash value of this hashing object as a string
|
||||||
containing 8-bit data. The object is not altered in any way
|
containing 8-bit data. The object is not altered in any way
|
||||||
by this function; you can continue updating the object after
|
by this function; you can continue updating the object after
|
||||||
calling this function.
|
calling this function.
|
||||||
|
|
||||||
|
hexdigest()
|
||||||
|
|
||||||
|
Return the hash value of this hashing object as a string
|
||||||
|
containing hexadecimal digits. Lowercase letters should be used
|
||||||
|
for the digits 'a' through 'f'. Like the .digest() method, this
|
||||||
|
method mustn't alter the object.
|
||||||
|
|
||||||
|
reset()
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
update(arg)
|
update(arg)
|
||||||
|
|
||||||
Update this hashing object with the string arg.
|
Update this hashing object with the string 'arg'.
|
||||||
|
|
||||||
For convenience, hashing objects also have a digest_size
|
For convenience, hashing objects also have a digest_size
|
||||||
attribute, measuring the size of the resulting digest in bytes.
|
attribute, measuring the size of the resulting digest in bytes.
|
||||||
|
@ -79,14 +106,29 @@ Specification
|
||||||
|
|
||||||
>>> from Crypto.Hash import MD5
|
>>> from Crypto.Hash import MD5
|
||||||
>>> m = MD5.new()
|
>>> m = MD5.new()
|
||||||
|
>>> m.digestsize
|
||||||
|
16
|
||||||
>>> m.update('abc')
|
>>> m.update('abc')
|
||||||
>>> m.digest()
|
>>> m.digest()
|
||||||
'\220\001P\230<\322O\260\326\226?@}(\341\177r'
|
'\x90\x01P\x98<\xd2O\xb0\xd6\x96?}(\xe1\x7fr'
|
||||||
|
>>> m.hexdigest()
|
||||||
|
'900150983cd24fb0d6963f7d28e17f72'
|
||||||
Or, more compactly:
|
Or, more compactly:
|
||||||
|
|
||||||
>>> MD5.new('abc').digest()
|
>>> MD5.new('abc').digest()
|
||||||
'\220\001P\230<\322O\260\326\226?@}(\341\177r'
|
'\x90\x01P\x98<\xd2O\xb0\xd6\x96?}(\xe1\x7fr'
|
||||||
|
|
||||||
|
|
||||||
|
Changes
|
||||||
|
|
||||||
|
2001-09-17: Renamed clear() to reset; added digestsize attribute
|
||||||
|
to objects; added .hexdigest() method.
|
||||||
|
|
||||||
|
|
||||||
|
Acknowledgements
|
||||||
|
|
||||||
|
Thanks to Andrew Archibald, Rich Salz, and the readers of the
|
||||||
|
python-crypto list for their comments on this PEP.
|
||||||
|
|
||||||
|
|
||||||
Copyright
|
Copyright
|
||||||
|
|
Loading…
Reference in New Issue