From e5013b26c449fd9b4afc839d02ae5b6b7e21088c Mon Sep 17 00:00:00 2001 From: Paul Marillonnet Date: Fri, 16 Mar 2018 17:53:06 +0100 Subject: [PATCH] pycrict: initial utils functions --- pycrict/README | 5 +++++ pycrict/pycrict/__init__.py | 0 pycrict/pycrict/rsa.py | 10 ++++++++++ pycrict/pycrict/settings.py | 2 ++ pycrict/pycrict/utils.py | 29 +++++++++++++++++++++++++++++ pycrict/setup.py | 24 ++++++++++++++++++++++++ pycrict/tests/testutils.py | 16 ++++++++++++++++ 7 files changed, 86 insertions(+) create mode 100644 pycrict/README create mode 100644 pycrict/pycrict/__init__.py create mode 100644 pycrict/pycrict/rsa.py create mode 100644 pycrict/pycrict/settings.py create mode 100644 pycrict/pycrict/utils.py create mode 100755 pycrict/setup.py create mode 100755 pycrict/tests/testutils.py diff --git a/pycrict/README b/pycrict/README new file mode 100644 index 0000000..ea42bd1 --- /dev/null +++ b/pycrict/README @@ -0,0 +1,5 @@ +Pedagogical purpose only + +Python (re-)implementation of the cryptographic features offered by Cryptic + +author: pmarillonnet(at)entrouvert(dot)com diff --git a/pycrict/pycrict/__init__.py b/pycrict/pycrict/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pycrict/pycrict/rsa.py b/pycrict/pycrict/rsa.py new file mode 100644 index 0000000..e1af63e --- /dev/null +++ b/pycrict/pycrict/rsa.py @@ -0,0 +1,10 @@ +import utils + +def generate_key_pair(): # todo + pass + +def encrypt_rsa(m, e, n): # todo + pass + +def decrypt_rsa(m, d, n): # todo + pass diff --git a/pycrict/pycrict/settings.py b/pycrict/pycrict/settings.py new file mode 100644 index 0000000..d275b82 --- /dev/null +++ b/pycrict/pycrict/settings.py @@ -0,0 +1,2 @@ +RANDOM_PRIME_LENGTH=10 +RANDOM_PRIME_ELECTION_TRIES=5*pow(10,9) diff --git a/pycrict/pycrict/utils.py b/pycrict/pycrict/utils.py new file mode 100644 index 0000000..e89b349 --- /dev/null +++ b/pycrict/pycrict/utils.py @@ -0,0 +1,29 @@ +from random import randint +from math import sqrt + +def rabin_miller(prime): # todo + pass + +def possible_factors(c): + yield 2 + i = 3 + while i < sqrt(c): + yield i + i = i+2 + +def is_prime(c): + for i in possible_factors(c): + if c%i == 0: + return False + return True + +def get_random_prime(): + is_prime = False + p_length = settings.RANDOM_PRIME_LENGTH + ntries = 0 + + while not is_prime and n < settings.RANDOM_PRIME_ELECTION_TRIES: + candidate = randint(pow(10, p_length), pow(10, p_length+1)) + if is_prime(candidate): + return candidate + n = n+1 diff --git a/pycrict/setup.py b/pycrict/setup.py new file mode 100755 index 0000000..8a6c635 --- /dev/null +++ b/pycrict/setup.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +from setuptools import setup, find_packages + +import pycrict + +setup( + name='pycrict', + packages=find_packages(), + author='Paul M.', + author_email='paul@nowhere.null', + description='Paul\'s simple crypto module', + long_description=open('README').read(), + include_package_data=False, + classifiers=[ + "Programming Language :: Python", + "Development Status :: 1 - Planning", + "License :: OSI Approved", + "Natural Language :: English", + "Operating System :: OS Independent", + "Programming Language :: Python :: 2.7", + "Topic :: Software Development :: Libraries :: Python Modules", + ] +) diff --git a/pycrict/tests/testutils.py b/pycrict/tests/testutils.py new file mode 100755 index 0000000..71b2a97 --- /dev/null +++ b/pycrict/tests/testutils.py @@ -0,0 +1,16 @@ +#!/usr/bin/python + +from pycrict import utils + +#todo pytestify + +def test_is_prime(): + assert utils.is_prime(523) + assert not utils.is_prime(527) + assert utils.is_prime(6491) + assert not utils.is_prime(6707) + assert utils.is_prime(5483983) + assert not utils.is_prime(5483987) + +test_is_prime() +print 'everything is fine' -- 2.16.2