Skip to content

Commit c427132

Browse files
authored
Merge pull request #9 from codesrg/develop
Develop
2 parents bd98489 + 6459064 commit c427132

File tree

8 files changed

+35
-26
lines changed

8 files changed

+35
-26
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ to encrypt/decrypt message:
3333

3434
### Python Script
3535
To encrypt/decrypt message using rsa.
36-
```
36+
```python
3737
from sipher import rsa
3838

3939
rsa.gen_keys()
@@ -44,7 +44,7 @@ signature = rsa.sign(message, privatekey)
4444
citext = rsa.encrypt(message, publickey)
4545

4646
decrypted_message = rsa.decrypt(citext, privatekey)
47-
verify(decrypted_message, signature, publickey)
47+
rsa.verify(decrypted_message, signature, publickey)
4848
```
4949

5050
### Command Line

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "sipher"
7-
version = "1.0.2"
7+
version = "1.0.3"
88
description = "To encrypt and decrypt message."
99
readme = "README.md"
1010
requires-python = ">=3.6"

sipher/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
morse = Morse()
1010

1111
__author__ = 'srg'
12-
__version__ = '1.0.2'
12+
__version__ = '1.0.3'
1313

1414
__all__ = [
1515
'rsa',

sipher/__main__.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import sys
45
import argparse
56
from srutil import util
67
from pathlib import Path
@@ -39,26 +40,38 @@ def get_argument():
3940
return options
4041

4142

42-
def encrypt(s: Sipher, data: str | os.PathLike, key, copy_to_clipboard: bool = False, store: bool = False,
43-
store_path: str = None):
44-
s.encrypt(data, key.__getitem__(1), copy_to_clipboard=copy_to_clipboard, store=store, store_path=store_path)
43+
def _remove_unwanted_params(s: Sipher, params: dict) -> dict:
44+
method_list = {'encrypt': s.encrypt, 'decrypt': s.decrypt}
45+
params_of_method = util.paramsofmethod(method_list.get(util.whocalledme())).keys()
46+
new_params = dict()
47+
for key, value in params.items():
48+
if key in params_of_method:
49+
new_params.setdefault(key, value)
50+
return new_params
4551

4652

47-
def decrypt(s: Sipher, data: str | os.PathLike, key, copy_to_clipboard: bool = False, store: bool = False,
48-
store_path: str = None):
49-
s.decrypt(data, key.__getitem__(0), copy_to_clipboard=copy_to_clipboard, store=store, store_path=store_path)
53+
def encrypt(s: Sipher, data: str | os.PathLike, **kwargs):
54+
kwargs = _remove_unwanted_params(s, kwargs)
55+
s.encrypt(data, **kwargs)
56+
57+
58+
def decrypt(s: Sipher, data: str | os.PathLike, **kwargs):
59+
kwargs = _remove_unwanted_params(s, kwargs)
60+
s.decrypt(data, **kwargs)
5061

5162

5263
def main():
5364
options = get_argument()
5465
sipher_alg = {'morse': morse, 'rsa': rsa, 'base64': base64}
5566
s = sipher_alg.get(options.alg)
56-
data = util.getinstanceof(options.data, Path) if os.path.exists(options.data) else options.data
67+
data = Path(options.data) if os.path.exists(options.data) else options.data
5768
if options.encrypt:
58-
encrypt(s, data, options.key, options.copy_to_clipboard, options.store, options.store_path)
69+
encrypt(s, data, pub_key=options.key.__getitem__(1), copy_to_clipboard=options.copy_to_clipboard,
70+
store=options.store, store_path=options.store_path)
5971
elif options.decrypt:
60-
decrypt(s, data, options.key, options.copy_to_clipboard, options.store, options.store_path)
72+
decrypt(s, data, priv_key=options.key.__getitem__(0), copy_to_clipboard=options.copy_to_clipboard,
73+
store=options.store, store_path=options.store_path)
6174

6275

6376
if __name__ == "__main__":
64-
main()
77+
sys.exit(main())

sipher/_base64.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
from typing import AnyStr, Optional
66

77
from sipher._sipher import Sipher
8-
from . import sipherutil as su
98

109

1110
class Base64(Sipher):
1211
def __init__(self):
1312
self.__em = ''
1413
self.__dm = ''
1514

16-
def encrypt(self, data: AnyStr | os.PathLike[AnyStr], key=None, copy_to_clipboard: bool = False,
15+
def encrypt(self, data: AnyStr | os.PathLike[AnyStr], copy_to_clipboard: bool = False,
1716
store: bool = False, store_path: Optional[str] = None):
1817
if isinstance(data, os.PathLike):
1918
data = self._get_data_from_file(data)
@@ -22,7 +21,7 @@ def encrypt(self, data: AnyStr | os.PathLike[AnyStr], key=None, copy_to_clipboar
2221
self._copy_store_m(self.__em, self, copy_to_clipboard, store, store_path, encryption=True)
2322
return self.__em
2423

25-
def decrypt(self, data: AnyStr | os.PathLike[AnyStr], key=None, copy_to_clipboard: bool = False,
24+
def decrypt(self, data: AnyStr | os.PathLike[AnyStr], copy_to_clipboard: bool = False,
2625
store: bool = False, store_path: Optional[str] = None):
2726
if isinstance(data, os.PathLike):
2827
data = self._get_data_from_file(data)

sipher/_morse.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from typing import AnyStr, Optional
55

66
from ._sipher import Sipher
7-
from . import sipherutil as su
87

98

109
class Morse(Sipher):
@@ -27,7 +26,7 @@ def __init__(self):
2726
self.__em = ''
2827
self.__dm = ''
2928

30-
def encrypt(self, data: AnyStr | PathLike[AnyStr], key=None, copy_to_clipboard: bool = False, store: bool = False,
29+
def encrypt(self, data: AnyStr | PathLike[AnyStr], copy_to_clipboard: bool = False, store: bool = False,
3130
store_path: Optional[str] = None):
3231
if isinstance(data, PathLike):
3332
data = self._get_data_from_file(data)
@@ -36,7 +35,7 @@ def encrypt(self, data: AnyStr | PathLike[AnyStr], key=None, copy_to_clipboard:
3635
self._copy_store_m(self.__em, self, copy_to_clipboard, store, store_path, encryption=True)
3736
return self.__em
3837

39-
def decrypt(self, data: AnyStr | PathLike[AnyStr], key=None, copy_to_clipboard: bool = False, store: bool = False,
38+
def decrypt(self, data: AnyStr | PathLike[AnyStr], copy_to_clipboard: bool = False, store: bool = False,
4039
store_path: Optional[str] = None):
4140
if isinstance(data, PathLike):
4241
data = self._get_data_from_file(data)

sipher/_rsa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ def decrypt(self, data: bytes | PathLike[bytes], priv_key: PrivateKey = None, co
6565
if isinstance(data, PathLike):
6666
data = self._get_data_from_file(data, mode='rb')
6767
self.__dm = rsa.decrypt(data, priv_key=priv_key)
68-
self._copy_store_m(self.__dm.decode('ascii'), self, copy_to_clipboard, store, store_path, 'wb', decryption=True)
68+
self._copy_store_m(self.__dm, self, copy_to_clipboard, store, store_path, 'wb', decryption=True)
6969
return self.__dm.decode('ascii')

sipher/_sipher.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import abc
55
from srutil import util
6-
from typing import AnyStr, Optional
6+
from typing import AnyStr
77

88
from . import sipherutil as su
99

@@ -32,11 +32,9 @@ def _copy_store_m(m, alg, copy: bool = False, store: bool = False, store_path=No
3232
print("{} message stored in '{}'".format(msg, path.__str__()).strip())
3333

3434
@abc.abstractmethod
35-
def encrypt(self, data: AnyStr | os.PathLike[AnyStr], key=None, copy_to_clipboard: bool = False,
36-
store: bool = False, store_path: Optional[str] = None) -> AnyStr:
35+
def encrypt(self, data: AnyStr | os.PathLike[AnyStr], **kwargs) -> AnyStr:
3736
pass
3837

3938
@abc.abstractmethod
40-
def decrypt(self, data: AnyStr | os.PathLike[AnyStr], key=None, copy_to_clipboard: bool = False,
41-
store: bool = False, store_path: Optional[str] = None) -> AnyStr:
39+
def decrypt(self, data: AnyStr | os.PathLike[AnyStr], **kwargs) -> AnyStr:
4240
pass

0 commit comments

Comments
 (0)