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 diff --git a/pyproject.toml b/pyproject.toml index 8e567ac..2404140 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" @@ -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 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', diff --git a/sipher/__main__.py b/sipher/__main__.py index 9acbb2d..50bab5e 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 @@ -39,26 +40,38 @@ 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, key.__getitem__(1), copy_to_clipboard=copy_to_clipboard, store=store, store_path=store_path) +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 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) +def encrypt(s: Sipher, data: str | os.PathLike, **kwargs): + kwargs = _remove_unwanted_params(s, kwargs) + s.encrypt(data, **kwargs) + + +def decrypt(s: Sipher, data: str | os.PathLike, **kwargs): + kwargs = _remove_unwanted_params(s, kwargs) + 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__": - 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/_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') 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 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()