diff options
author | Legrandin <gooksankoo@hoiptorrow.mailexpire.com> | 2011-02-07 22:32:49 +0100 |
---|---|---|
committer | Legrandin <gooksankoo@hoiptorrow.mailexpire.com> | 2011-02-07 22:32:49 +0100 |
commit | 07e9e97e9d1844e5b5427a5e9164ff848eacde19 (patch) | |
tree | 62cd305feffcfc9a058147cc80f7e8b5d1ace149 | |
parent | e34af945eac49012be1284b9d948a54c0aabd32e (diff) | |
download | pycrypto-07e9e97e9d1844e5b5427a5e9164ff848eacde19.tar.gz |
Add new() method to all remaining hash algorithms, so as to make them to work with PKCS#1 PSS. Add also test cases for it for every hash.
-rw-r--r-- | lib/Crypto/Random/Fortuna/SHAd256.py | 4 | ||||
-rw-r--r-- | lib/Crypto/SelfTest/Hash/common.py | 6 | ||||
-rw-r--r-- | src/hash_template.c | 6 |
3 files changed, 15 insertions, 1 deletions
diff --git a/lib/Crypto/Random/Fortuna/SHAd256.py b/lib/Crypto/Random/Fortuna/SHAd256.py index 288a53e..8b63cb2 100644 --- a/lib/Crypto/Random/Fortuna/SHAd256.py +++ b/lib/Crypto/Random/Fortuna/SHAd256.py @@ -83,6 +83,8 @@ digest_size = _SHAd256.digest_size # PEP 247 module-level "new" function def new(data=""): """Return a new SHAd256 hashing object""" - return _SHAd256(_SHAd256._internal, SHA256.new(data)) + sha = _SHAd256(_SHAd256._internal, SHA256.new(data)) + sha.new = globals()['new'] + return sha # vim:set ts=4 sw=4 sts=4 expandtab: diff --git a/lib/Crypto/SelfTest/Hash/common.py b/lib/Crypto/SelfTest/Hash/common.py index 555ee7b..90387b2 100644 --- a/lib/Crypto/SelfTest/Hash/common.py +++ b/lib/Crypto/SelfTest/Hash/common.py @@ -70,6 +70,12 @@ class HashSelfTest(unittest.TestCase): self.assertEqual(self.expected, out3) # h = .new(data); h.hexdigest() self.assertEqual(self.expected, out4) # h = .new(data); h.digest() + # Verify that new() object method produces a fresh hash object + h2 = h.new() + h2.update(self.input) + out5 = binascii.b2a_hex(h2.digest()) + self.assertEqual(self.expected, out5) + class HashTestOID(unittest.TestCase): def __init__(self, hashmod, oid): unittest.TestCase.__init__(self) diff --git a/src/hash_template.c b/src/hash_template.c index 1ddaab2..c3e651f 100644 --- a/src/hash_template.c +++ b/src/hash_template.c @@ -157,12 +157,17 @@ ALG_update(ALGobject *self, PyObject *args) return Py_None; } +/** Forward declaration for this module's new() method **/ +static char ALG_new__doc__[]; +static PyObject *ALG_new(PyObject*, PyObject*); + static PyMethodDef ALG_methods[] = { {"copy", (PyCFunction)ALG_copy, METH_VARARGS, ALG_copy__doc__}, {"digest", (PyCFunction)ALG_digest, METH_VARARGS, ALG_digest__doc__}, {"hexdigest", (PyCFunction)ALG_hexdigest, METH_VARARGS, ALG_hexdigest__doc__}, {"update", (PyCFunction)ALG_update, METH_VARARGS, ALG_update__doc__}, + {"new", (PyCFunction)ALG_new, METH_VARARGS, ALG_new__doc__}, {NULL, NULL} /* sentinel */ }; @@ -206,6 +211,7 @@ static char ALG_new__doc__[] = "argument may be provided; if present, this string will be " "automatically hashed into the initial state of the object."; +/** This method belong to both the module and the hash object **/ static PyObject * ALG_new(PyObject *self, PyObject *args) { |