From 3f808fbcd772c138f744d551114688fa22363609 Mon Sep 17 00:00:00 2001 From: Kashiko Koibumi Date: Sat, 18 May 2024 12:13:24 +0900 Subject: [PATCH] fix to pass tests.py * deterministic_keys() now accepts both str and bytes for passphrase. * The test for RandomTrackingDict is modified to use bytes for random keys. --- src/highlevelcrypto.py | 7 ++++++- src/tests/test_randomtrackingdict.py | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/highlevelcrypto.py b/src/highlevelcrypto.py index 551aa376..e0b86f35 100644 --- a/src/highlevelcrypto.py +++ b/src/highlevelcrypto.py @@ -85,7 +85,12 @@ def random_keys(): def deterministic_keys(passphrase, nonce): """Generate keys from *passphrase* and *nonce* (encoded as varint)""" - priv = hashlib.sha512(passphrase.encode() + nonce).digest()[:32] + if isinstance(passphrase, str): + ph = passphrase.encode() + else: + ph = passphrase + priv = hashlib.sha512(ph + nonce).digest()[:32] + pub = pointMult(priv) return priv, pub diff --git a/src/tests/test_randomtrackingdict.py b/src/tests/test_randomtrackingdict.py index 2db3c423..cbe0ee55 100644 --- a/src/tests/test_randomtrackingdict.py +++ b/src/tests/test_randomtrackingdict.py @@ -15,10 +15,10 @@ class TestRandomTrackingDict(unittest.TestCase): @staticmethod def randString(): """helper function for tests, generates a random string""" - retval = '' - for _ in range(32): - retval += chr(random.randint(0, 255)) - return retval + retval = bytearray(32) + for i in range(32): + retval[i] = random.randint(0, 255) + return bytes(retval) def test_check_randomtrackingdict(self): """Check the logic of RandomTrackingDict class"""