From 91c73c042ea3cb00d50250212f126be0a6892778 Mon Sep 17 00:00:00 2001 From: codesrg Date: Sun, 20 Nov 2022 08:57:31 +0530 Subject: [PATCH 1/7] optimization --- sipher/__main__.py | 8 +++++--- sipher/_base64.py | 5 ++--- sipher/_morse.py | 5 ++--- sipher/_sipher.py | 8 +++----- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/sipher/__main__.py b/sipher/__main__.py index 9acbb2d..cd2aa71 100644 --- a/sipher/__main__.py +++ b/sipher/__main__.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import sys import argparse from srutil import util from pathlib import Path @@ -41,12 +42,13 @@ def get_argument(): def encrypt(s: Sipher, data: str | os.PathLike, key, copy_to_clipboard: bool = False, store: bool = False, store_path: str = None): - s.encrypt(data, key.__getitem__(1), copy_to_clipboard=copy_to_clipboard, store=store, store_path=store_path) + s.encrypt(data, pub_key=key.__getitem__(1), copy_to_clipboard=copy_to_clipboard, store=store, store_path=store_path) def decrypt(s: Sipher, data: str | os.PathLike, key, copy_to_clipboard: bool = False, store: bool = False, store_path: str = None): - s.decrypt(data, key.__getitem__(0), copy_to_clipboard=copy_to_clipboard, store=store, store_path=store_path) + s.decrypt(data, priv_key=key.__getitem__(0), copy_to_clipboard=copy_to_clipboard, store=store, + store_path=store_path) def main(): @@ -61,4 +63,4 @@ def main(): if __name__ == "__main__": - main() + sys.exit(main()) diff --git a/sipher/_base64.py b/sipher/_base64.py index 693fd9b..f9df66a 100644 --- a/sipher/_base64.py +++ b/sipher/_base64.py @@ -5,7 +5,6 @@ from typing import AnyStr, Optional from sipher._sipher import Sipher -from . import sipherutil as su class Base64(Sipher): @@ -13,7 +12,7 @@ def __init__(self): self.__em = '' self.__dm = '' - def encrypt(self, data: AnyStr | os.PathLike[AnyStr], key=None, copy_to_clipboard: bool = False, + def encrypt(self, data: AnyStr | os.PathLike[AnyStr], copy_to_clipboard: bool = False, store: bool = False, store_path: Optional[str] = None): if isinstance(data, os.PathLike): data = self._get_data_from_file(data) @@ -22,7 +21,7 @@ def encrypt(self, data: AnyStr | os.PathLike[AnyStr], key=None, copy_to_clipboar self._copy_store_m(self.__em, self, copy_to_clipboard, store, store_path, encryption=True) return self.__em - def decrypt(self, data: AnyStr | os.PathLike[AnyStr], key=None, copy_to_clipboard: bool = False, + def decrypt(self, data: AnyStr | os.PathLike[AnyStr], copy_to_clipboard: bool = False, store: bool = False, store_path: Optional[str] = None): if isinstance(data, os.PathLike): data = self._get_data_from_file(data) diff --git a/sipher/_morse.py b/sipher/_morse.py index a9a5c0e..50d4158 100644 --- a/sipher/_morse.py +++ b/sipher/_morse.py @@ -4,7 +4,6 @@ from typing import AnyStr, Optional from ._sipher import Sipher -from . import sipherutil as su class Morse(Sipher): @@ -27,7 +26,7 @@ def __init__(self): self.__em = '' self.__dm = '' - def encrypt(self, data: AnyStr | PathLike[AnyStr], key=None, copy_to_clipboard: bool = False, store: bool = False, + def encrypt(self, data: AnyStr | PathLike[AnyStr], copy_to_clipboard: bool = False, store: bool = False, store_path: Optional[str] = None): if isinstance(data, PathLike): data = self._get_data_from_file(data) @@ -36,7 +35,7 @@ def encrypt(self, data: AnyStr | PathLike[AnyStr], key=None, copy_to_clipboard: self._copy_store_m(self.__em, self, copy_to_clipboard, store, store_path, encryption=True) return self.__em - def decrypt(self, data: AnyStr | PathLike[AnyStr], key=None, copy_to_clipboard: bool = False, store: bool = False, + def decrypt(self, data: AnyStr | PathLike[AnyStr], copy_to_clipboard: bool = False, store: bool = False, store_path: Optional[str] = None): if isinstance(data, PathLike): data = self._get_data_from_file(data) diff --git a/sipher/_sipher.py b/sipher/_sipher.py index 0f00ce3..0495e19 100644 --- a/sipher/_sipher.py +++ b/sipher/_sipher.py @@ -3,7 +3,7 @@ import os import abc from srutil import util -from typing import AnyStr, Optional +from typing import AnyStr from . import sipherutil as su @@ -32,11 +32,9 @@ def _copy_store_m(m, alg, copy: bool = False, store: bool = False, store_path=No print("{} message stored in '{}'".format(msg, path.__str__()).strip()) @abc.abstractmethod - def encrypt(self, data: AnyStr | os.PathLike[AnyStr], key=None, copy_to_clipboard: bool = False, - store: bool = False, store_path: Optional[str] = None) -> AnyStr: + def encrypt(self, data: AnyStr | os.PathLike[AnyStr], **kwargs) -> AnyStr: pass @abc.abstractmethod - def decrypt(self, data: AnyStr | os.PathLike[AnyStr], key=None, copy_to_clipboard: bool = False, - store: bool = False, store_path: Optional[str] = None) -> AnyStr: + def decrypt(self, data: AnyStr | os.PathLike[AnyStr], **kwargs) -> AnyStr: pass From 998a4c1fea7c9a805365d2488bb252b4899fed71 Mon Sep 17 00:00:00 2001 From: codesrg Date: Sun, 20 Nov 2022 09:14:51 +0530 Subject: [PATCH 2/7] version updated --- pyproject.toml | 2 +- sipher/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8e567ac..1983bde 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "sipher" -version = "1.0.2" +version = "1.0.3" description = "To encrypt and decrypt message." readme = "README.md" requires-python = ">=3.6" diff --git a/sipher/__init__.py b/sipher/__init__.py index 36606d1..0806aeb 100644 --- a/sipher/__init__.py +++ b/sipher/__init__.py @@ -9,7 +9,7 @@ morse = Morse() __author__ = 'srg' -__version__ = '1.0.2' +__version__ = '1.0.3' __all__ = [ 'rsa', From 35b0874006d44265574b94ee6c66cecb4df67b3b Mon Sep 17 00:00:00 2001 From: codesrg Date: Sun, 20 Nov 2022 09:20:46 +0530 Subject: [PATCH 3/7] README.md updated --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 27bd0fe..d8c5479 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ to encrypt/decrypt message: ### Python Script To encrypt/decrypt message using rsa. -``` +```python from sipher import rsa rsa.gen_keys() @@ -44,7 +44,7 @@ signature = rsa.sign(message, privatekey) citext = rsa.encrypt(message, publickey) decrypted_message = rsa.decrypt(citext, privatekey) -verify(decrypted_message, signature, publickey) +rsa.verify(decrypted_message, signature, publickey) ``` ### Command Line From 6f19f80c8bc4f05f50ab9301fb4e8b532ebd96ab Mon Sep 17 00:00:00 2001 From: codesrg Date: Sun, 20 Nov 2022 11:21:07 +0530 Subject: [PATCH 4/7] error fixes --- sipher/__main__.py | 23 +++++++++++++---------- sipher/_rsa.py | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/sipher/__main__.py b/sipher/__main__.py index cd2aa71..1fff48d 100644 --- a/sipher/__main__.py +++ b/sipher/__main__.py @@ -40,26 +40,29 @@ def get_argument(): return options -def encrypt(s: Sipher, data: str | os.PathLike, key, copy_to_clipboard: bool = False, store: bool = False, - store_path: str = None): - s.encrypt(data, pub_key=key.__getitem__(1), copy_to_clipboard=copy_to_clipboard, store=store, store_path=store_path) +def encrypt(s: Sipher, data: str | os.PathLike, **kwargs): + if s.__class__.__name__ != rsa.__class__.__name__: + kwargs.pop("pub_key") + s.encrypt(data, **kwargs) -def decrypt(s: Sipher, data: str | os.PathLike, key, copy_to_clipboard: bool = False, store: bool = False, - store_path: str = None): - s.decrypt(data, priv_key=key.__getitem__(0), copy_to_clipboard=copy_to_clipboard, store=store, - store_path=store_path) +def decrypt(s: Sipher, data: str | os.PathLike, **kwargs): + if s.__class__.__name__ != rsa.__class__.__name__: + kwargs.pop("priv_key") + s.decrypt(data, **kwargs) def main(): options = get_argument() sipher_alg = {'morse': morse, 'rsa': rsa, 'base64': base64} s = sipher_alg.get(options.alg) - data = util.getinstanceof(options.data, Path) if os.path.exists(options.data) else options.data + data = Path(options.data) if os.path.exists(options.data) else options.data if options.encrypt: - encrypt(s, data, options.key, options.copy_to_clipboard, options.store, options.store_path) + encrypt(s, data, pub_key=options.key.__getitem__(1), copy_to_clipboard=options.copy_to_clipboard, + store=options.store, store_path=options.store_path) elif options.decrypt: - decrypt(s, data, options.key, options.copy_to_clipboard, options.store, options.store_path) + decrypt(s, data, priv_key=options.key.__getitem__(0), copy_to_clipboard=options.copy_to_clipboard, + store=options.store, store_path=options.store_path) if __name__ == "__main__": diff --git a/sipher/_rsa.py b/sipher/_rsa.py index 97c5f47..c0412c6 100644 --- a/sipher/_rsa.py +++ b/sipher/_rsa.py @@ -65,5 +65,5 @@ def decrypt(self, data: bytes | PathLike[bytes], priv_key: PrivateKey = None, co if isinstance(data, PathLike): data = self._get_data_from_file(data, mode='rb') self.__dm = rsa.decrypt(data, priv_key=priv_key) - self._copy_store_m(self.__dm.decode('ascii'), self, copy_to_clipboard, store, store_path, 'wb', decryption=True) + self._copy_store_m(self.__dm, self, copy_to_clipboard, store, store_path, 'wb', decryption=True) return self.__dm.decode('ascii') From 6459064f4610864e613679ae13e97d06a0235dfa Mon Sep 17 00:00:00 2001 From: codesrg Date: Sun, 20 Nov 2022 12:08:06 +0530 Subject: [PATCH 5/7] error fixes --- sipher/__main__.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/sipher/__main__.py b/sipher/__main__.py index 1fff48d..50bab5e 100644 --- a/sipher/__main__.py +++ b/sipher/__main__.py @@ -40,15 +40,23 @@ def get_argument(): return options +def _remove_unwanted_params(s: Sipher, params: dict) -> dict: + method_list = {'encrypt': s.encrypt, 'decrypt': s.decrypt} + params_of_method = util.paramsofmethod(method_list.get(util.whocalledme())).keys() + new_params = dict() + for key, value in params.items(): + if key in params_of_method: + new_params.setdefault(key, value) + return new_params + + def encrypt(s: Sipher, data: str | os.PathLike, **kwargs): - if s.__class__.__name__ != rsa.__class__.__name__: - kwargs.pop("pub_key") + kwargs = _remove_unwanted_params(s, kwargs) s.encrypt(data, **kwargs) def decrypt(s: Sipher, data: str | os.PathLike, **kwargs): - if s.__class__.__name__ != rsa.__class__.__name__: - kwargs.pop("priv_key") + kwargs = _remove_unwanted_params(s, kwargs) s.decrypt(data, **kwargs) From 727e3308348af53ac4a04f13d3fa3de3454c4ac0 Mon Sep 17 00:00:00 2001 From: codesrg Date: Mon, 21 Nov 2022 22:42:25 +0530 Subject: [PATCH 6/7] error fixes --- sipher/sipherutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sipher/sipherutil.py b/sipher/sipherutil.py index cb815c4..8cd455d 100644 --- a/sipher/sipherutil.py +++ b/sipher/sipherutil.py @@ -33,7 +33,7 @@ def retrieve(file_name: str, path: str, file_format: str = None, mode: str = 'r' path = os.getcwd() if not file_format: file_format = '' - path += "/{}.{}".format(file_name, file_format) + path += "/{}.{}".format(file_name, file_format).rstrip('.') file = Path(path) with open(file, mode) as f: to_return = f.read() From 393194a0e5d06be9e5f53b081485ef10809e200b Mon Sep 17 00:00:00 2001 From: codesrg Date: Sun, 27 Nov 2022 11:52:33 +0530 Subject: [PATCH 7/7] optimize imports --- pyproject.toml | 1 - requirements.txt | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1983bde..2404140 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,6 @@ authors = [ { name = "srg", email = "srg.code@pm.me" }, ] dependencies = [ - "pyperclip", "srutil", "rsa" ] diff --git a/requirements.txt b/requirements.txt index 3476bb2..526edad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ rsa~=4.8 -srutil~=1.0.0 -pyperclip==1.8.2 +srutil~=1.0.0 \ No newline at end of file