From 1c42ac38514b9a0aa280fadf791283cdcf4fc733 Mon Sep 17 00:00:00 2001 From: Connectety-T Date: Mon, 10 Apr 2023 21:02:54 +0200 Subject: [PATCH 1/3] add ability to submit factors. --- factordb/factordb.py | 8 ++++++++ tests/test_factordb.py | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/factordb/factordb.py b/factordb/factordb.py index 8358279..b17633b 100644 --- a/factordb/factordb.py +++ b/factordb/factordb.py @@ -49,3 +49,11 @@ def get_factor_list(self): return [] ml = [[int(x)] * y for x, y in factors] return [y for x in ml for y in x] + + @staticmethod + def submit_factors(product, factors): + headers = {"Content-Type": "application/x-www-form-urlencoded"} + + factors = map(str, factors) + data = {"report": f"{product}={','.join(factors)}"} + requests.post("http://factordb.com/report.php", headers=headers, data=data) diff --git a/tests/test_factordb.py b/tests/test_factordb.py index 32298a4..c002c39 100644 --- a/tests/test_factordb.py +++ b/tests/test_factordb.py @@ -3,6 +3,7 @@ import unittest from factordb.factordb import FactorDB +from Crypto.Util import number class FactorDBTestCase(unittest.TestCase): @@ -63,6 +64,28 @@ def test_factordb_api_prime(self): self.assertTrue(factordb.is_prime()) + def test_submit(self): + def generate_unfactorised_nums(): + p = number.getPrime(1024) + q = number.getPrime(1024) + n = p * q + + factordb = FactorDB(n) + factordb.connect() + + if factordb.get_status() == 'C': + return n, sorted([p, q]) + + return generate_unfactorised_nums() + + n, factors = generate_unfactorised_nums() + FactorDB.submit_factors(n, factors) + + factordb = FactorDB(n) + factordb.connect(reconnect=True) + self.assertEqual(factordb.get_status(), 'FF') + self.assertListEqual(factordb.get_factor_list(), factors) + def __check_testcase(self, factordb, expected): self.assertEqual(factordb.get_id(), expected['id']) self.assertEqual(factordb.get_status(), expected['status']) From 661e202f418e5252abf970599cc735f5f230a756 Mon Sep 17 00:00:00 2001 From: Connectety-T Date: Mon, 10 Apr 2023 21:16:04 +0200 Subject: [PATCH 2/3] use loop instead of recursion and thus remove one request to factordb. --- tests/test_factordb.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/test_factordb.py b/tests/test_factordb.py index c002c39..54958d4 100644 --- a/tests/test_factordb.py +++ b/tests/test_factordb.py @@ -65,7 +65,9 @@ def test_factordb_api_prime(self): self.assertTrue(factordb.is_prime()) def test_submit(self): - def generate_unfactorised_nums(): + # generate not yet factorized numbers + factordb = None + while factordb is None or factordb.get_status() != 'C': p = number.getPrime(1024) q = number.getPrime(1024) n = p * q @@ -73,15 +75,10 @@ def generate_unfactorised_nums(): factordb = FactorDB(n) factordb.connect() - if factordb.get_status() == 'C': - return n, sorted([p, q]) - - return generate_unfactorised_nums() - - n, factors = generate_unfactorised_nums() + # sort numbers because factordb returns them in ascending order + factors = sorted([p, q]) FactorDB.submit_factors(n, factors) - factordb = FactorDB(n) factordb.connect(reconnect=True) self.assertEqual(factordb.get_status(), 'FF') self.assertListEqual(factordb.get_factor_list(), factors) From 2f185578d30db0480bc1219a1a48d6fc23a3e5eb Mon Sep 17 00:00:00 2001 From: Connectety-T Date: Mon, 10 Apr 2023 21:26:59 +0200 Subject: [PATCH 3/3] add dev-requirements.txt. --- dev-requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 dev-requirements.txt diff --git a/dev-requirements.txt b/dev-requirements.txt new file mode 100644 index 0000000..acdfd20 --- /dev/null +++ b/dev-requirements.txt @@ -0,0 +1 @@ +pycryptodome