From aaf8c7aa5f90b6d58c0ee317e19976972eee39ba Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Mon, 14 Oct 2019 01:03:25 +0100 Subject: [PATCH] algorithms.c (ShakeNN.done): Offer a (sensible) default hash size. Organization: Straylight/Edgeware From: Mark Wooding --- algorithms.c | 10 ++++++---- t/t-algorithms.py | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/algorithms.c b/algorithms.c index dd0bd0a..42dbe6f 100644 --- a/algorithms.c +++ b/algorithms.c @@ -3063,11 +3063,13 @@ end: return (0); } -static PyObject *shakemeth_done(PyObject *me, PyObject *arg) +static PyObject *shakemeth_done(PyObject *me, PyObject *arg, PyObject *kw) { PyObject *rc = 0; - size_t n; - if (!PyArg_ParseTuple(arg, "O&:done", convszt, &n)) goto end; + size_t n = 100 - SHAKE_H(me)->h.r/2; + static const char *const kwlist[] = { "hsz", 0 }; + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:done", KWLIST, convszt, &n)) + goto end; if (shake_check(me, 0)) goto end; rc = bytestring_pywrap(0, n); shake_done(SHAKE_H(me), BIN_PTR(rc), n); @@ -3147,7 +3149,7 @@ static const PyMethodDef shake_pymethods[] = { #undef METHBUF_ METH (hashstrz, "S.hashstrz(STRING)") NAMETH(xof, "S.xof()") - METH (done, "S.done(LEN) -> H") + KWMETH(done, "S.done([hsz = CAP]) -> H") METH (get, "S.get(LEN) -> H") METH (mask, "S.mask(M) -> C") #undef METHNAME diff --git a/t/t-algorithms.py b/t/t-algorithms.py index bf637ef..3301228 100644 --- a/t/t-algorithms.py +++ b/t/t-algorithms.py @@ -797,14 +797,14 @@ class TestKeccak (HashBufferTestMixin): ## Check the menagerie of random hashing methods. def mkhash(_): x = xcls(func = func, perso = perso) - return x, lambda: x.done(100 - x.rate//2) + return x, x.done me.check_hashbuffer(mkhash) ## Check the state machine tracking. x = xcls(); me.assertEqual(x.state, "absorb") x.hash(m); me.assertEqual(x.state, "absorb") xx = x.copy() - h = xx.done(100 - x.rate//2) + h = xx.done(); me.assertEqual(len(h), 100 - x.rate//2) me.assertEqual(xx.state, "dead") me.assertRaises(ValueError, xx.done, 1) me.assertRaises(ValueError, xx.get, 1) -- [mdw]