passphera-core 0.1.0__tar.gz → 0.2.0__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: passphera-core
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: The core system of passphera project
5
5
  Home-page: https://github.com/passphera/core
6
6
  Author: Fathi Abdelmalek
@@ -0,0 +1,231 @@
1
+ from cipherspy.cipher import *
2
+
3
+
4
+ class InvalidAlgorithmException(Exception):
5
+ def __init__(self, algorithm: str) -> None:
6
+ super().__init__(f"Invalid algorithm name {algorithm}")
7
+
8
+
9
+ class PasswordGenerator:
10
+ """
11
+ A strong password generator use multiple cipher algorithms to cipher a given plain text
12
+ """
13
+ def __init__(
14
+ self,
15
+ text: str = None,
16
+ shift: int = 3,
17
+ multiplier: int = 3,
18
+ key_str: str = "secret",
19
+ key_iter: iter = (9, 4, 5, 7),
20
+ algorithm: str = 'playfair'
21
+ ):
22
+ """
23
+ :param text: plain text to be ciphered
24
+ :param shift: number of characters to shift each character (default 3)
25
+ :param multiplier: number of characters to shift each character (default 3)
26
+ :param key_str: cipher key string (default "secret")
27
+ :param key_iter: cipher key matrix (default (9, 4, 5, 7))
28
+ :param algorithm: main cipher algorithm name (default 'playfair')
29
+ """
30
+ self._chars_replacements: dict = {}
31
+ self._text: str = text
32
+ self._shift: int = shift
33
+ self._multiplier: int = multiplier
34
+ self._key_str: str = key_str
35
+ self._key_iter: iter = key_iter
36
+ self._algorithm_name: str = algorithm.lower()
37
+ self._algorithm = self._set_algorithm()
38
+ self._password: str = f'secret{self._text}secret'
39
+
40
+ @property
41
+ def text(self) -> str:
42
+ """
43
+ Returns the text to be ciphered into a password
44
+ Eg: ```password = pg.text```
45
+ :return: str: The text to be ciphered into a password
46
+ """
47
+ return self._text
48
+
49
+ @text.setter
50
+ def text(self, text: str) -> None:
51
+ """
52
+ Sets the text to be ciphered into a password
53
+ Eg: ```pg.text = 'secret 2024 password'```
54
+ :param text: The text to be ciphered into a password
55
+ :return:
56
+ """
57
+ self._text = text
58
+ self._password: str = f'secret{self._text}secret'
59
+
60
+ @property
61
+ def shift(self) -> int:
62
+ """
63
+ Returns the shift value for the cipher algorithm
64
+ Eg: ```shift = pg.shift```
65
+ :return: int: The shift value for the cipher algorithm
66
+ """
67
+ return self._shift
68
+
69
+ @shift.setter
70
+ def shift(self, shift: int) -> None:
71
+ """
72
+ Sets the shift value for the cipher algorithm
73
+ Eg: ```pg.shift = 3```
74
+ :param shift: The shift value for the cipher algorithm
75
+ :return:
76
+ """
77
+ self._shift = shift
78
+
79
+ @property
80
+ def multiplier(self) -> int:
81
+ """
82
+ Returns the multiplier value for the cipher algorithm
83
+ Eg: ```multiplier = pg.multiplier```
84
+ :return: int: The multiplier value for the cipher algorithm
85
+ """
86
+ return self._multiplier
87
+
88
+ @multiplier.setter
89
+ def multiplier(self, multiplier: int) -> None:
90
+ """
91
+ Sets the multiplier value for the cipher algorithm
92
+ Eg: ```pg.multiplier = 3```
93
+ :param multiplier: The multiplier value for the cipher algorithm
94
+ :return:
95
+ """
96
+ self._multiplier = multiplier
97
+
98
+ @property
99
+ def key_str(self) -> str:
100
+ """
101
+ Returns the key string for the cipher algorithm
102
+ Eg: ```key_str = pg.key_str```
103
+ :return: str: The key string for the cipher algorithm
104
+ """
105
+ return self._key_str
106
+
107
+ @key_str.setter
108
+ def key_str(self, key_str: str) -> None:
109
+ """
110
+ Sets the key string for the cipher algorithm
111
+ Eg: ```pg.key_str = 'secret key'```
112
+ :param key_str: The key string for the cipher algorithm
113
+ :return:
114
+ """
115
+ self._key_str = key_str
116
+
117
+ @property
118
+ def key_iter(self) -> iter:
119
+ """
120
+ Returns the key matrix for the cipher algorithm
121
+ Eg: ```key_iter = pg.key_iter```
122
+ :return: iter: The key matrix for the cipher algorithm
123
+ """
124
+ return self._key_iter
125
+
126
+ @key_iter.setter
127
+ def key_iter(self, key_iter: iter) -> None:
128
+ """
129
+ Sets the key matrix for the cipher algorithm
130
+ Eg: ```pg.key_iter = (9, 5, 2, 4)```
131
+ :param key_iter: The key matrix for the cipher algorithm
132
+ :return:
133
+ """
134
+ self._key_iter = key_iter
135
+
136
+ @property
137
+ def algorithm(self) -> str:
138
+ """
139
+ Returns the main cipher algorithm name
140
+ Eg: ```algorithm = pg.algorithm```
141
+ :return: str: The main cipher algorithm name
142
+ """
143
+ return self._algorithm_name
144
+
145
+ @algorithm.setter
146
+ def algorithm(self, algorithm: str) -> None:
147
+ """
148
+ Sets the main cipher algorithm
149
+ Eg: ```pg.algorithm = 'playfair'```
150
+ :param algorithm: The name of the main cipher algorithm
151
+ :return:
152
+ """
153
+ self._algorithm_name = algorithm.lower()
154
+ self._algorithm = self._set_algorithm()
155
+
156
+ @property
157
+ def characters_replacements(self) -> dict:
158
+ """
159
+ Returns the dictionary of the characters replacements
160
+ Eg: ```print(pg.characters_replacements) # {'a': '@1', 'b': '#2'}```
161
+ :return: dict: The dictionary of the characters replacements
162
+ """
163
+ return self._chars_replacements
164
+
165
+ def _set_algorithm(self):
166
+ """
167
+ Return new instance of the used algorithm to the given one by it's name
168
+ :return: new algorithm class
169
+ """
170
+ match self._algorithm_name:
171
+ case 'caesar':
172
+ return CaesarCipher(self._shift)
173
+ case 'affine':
174
+ return AffineCipher(self._multiplier, self._shift)
175
+ case 'playfair':
176
+ return PlayfairCipher(self._key_str)
177
+ case 'hill':
178
+ return HillCipher(self._key_iter)
179
+ case _:
180
+ raise InvalidAlgorithmException(self._algorithm_name)
181
+
182
+ def _update_algorithm_properties(self) -> None:
183
+ """
184
+ Update the main cipher algorithm
185
+ """
186
+ self._algorithm = self._set_algorithm()
187
+
188
+ def replace_character(self, char: str, replacement: str) -> None:
189
+ """
190
+ Replace a character with another character or set of characters
191
+ Eg: pg.replace_character('a', '@1')
192
+ :param char: The character to be replaced
193
+ :param replacement: The (character|set of characters) to replace the first one
194
+ :return:
195
+ """
196
+ self._chars_replacements[char[0]] = replacement
197
+
198
+ def reset_character(self, char: str) -> None:
199
+ """
200
+ Reset a character to it's original value (remove it's replacement from characters_replacements)
201
+ :param char: The character to be reset to its original value
202
+ :return:
203
+ """
204
+ if char in self._chars_replacements:
205
+ del self._chars_replacements[char]
206
+
207
+ def generate_raw_password(self) -> str:
208
+ """
209
+ Generate a raw password string using the given parameters
210
+ :return: str: The generated raw password
211
+ """
212
+ self._update_algorithm_properties()
213
+ return self._algorithm.encrypt(self._password)
214
+
215
+ def generate_password(self) -> str:
216
+ """
217
+ Generate a strong password string using the raw password (add another layer of encryption to it)
218
+ :return: str: The generated strong password
219
+ """
220
+ old_algorithm = self._algorithm_name
221
+ self._algorithm_name = 'affine'
222
+ self._update_algorithm_properties()
223
+ self._password = self.generate_raw_password()
224
+ self._algorithm_name = old_algorithm
225
+ self._update_algorithm_properties()
226
+ for char, replacement in self._chars_replacements.items():
227
+ self._password = self._password.replace(char, replacement)
228
+ for char in self._password:
229
+ if char in self._text:
230
+ self._password = self._text.replace(char, char.upper())
231
+ return self._password
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: passphera-core
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: The core system of passphera project
5
5
  Home-page: https://github.com/passphera/core
6
6
  Author: Fathi Abdelmalek
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setup(
7
7
  name='passphera-core',
8
- version='0.1.0',
8
+ version='0.2.0',
9
9
  author='Fathi Abdelmalek',
10
10
  author_email='abdelmalek.fathi.2001@gmail.com',
11
11
  url='https://github.com/passphera/core',
@@ -1,117 +0,0 @@
1
- from cipherspy.cipher import *
2
-
3
-
4
- class InvalidAlgorithmException(Exception):
5
- def __init__(self, algorithm: str) -> None:
6
- super().__init__(f"Invalid algorithm name {algorithm}")
7
-
8
-
9
- class PasswordGenerator:
10
- def __init__(
11
- self,
12
- text: str = None,
13
- shift: int = 3,
14
- multiplier: int = 3,
15
- key_str: str = "secret",
16
- key_iter: iter = (9, 4, 5, 7),
17
- algorithm: str = 'playfair'
18
- ):
19
- self._char_replacements: dict = {}
20
- self._text: str = text
21
- self._shift: int = shift
22
- self._multiplier: int = multiplier
23
- self._key_str: str = key_str
24
- self._key_iter: iter = key_iter
25
- self._algorithm_name: str = algorithm.lower()
26
- self._algorithm = self._set_algorithm()
27
-
28
- @property
29
- def text(self) -> str:
30
- return self._text
31
-
32
- @text.setter
33
- def text(self, text: str) -> None:
34
- self._text = text
35
-
36
- @property
37
- def shift(self) -> int:
38
- return self._shift
39
-
40
- @shift.setter
41
- def shift(self, shift: int) -> None:
42
- self._shift = shift
43
-
44
- @property
45
- def multiplier(self) -> int:
46
- return self._multiplier
47
-
48
- @multiplier.setter
49
- def multiplier(self, multiplier: int) -> None:
50
- self._multiplier = multiplier
51
-
52
- @property
53
- def key_str(self) -> str:
54
- return self._key_str
55
-
56
- @key_str.setter
57
- def key_str(self, key_str: str) -> None:
58
- self._key_str = key_str
59
-
60
- @property
61
- def key_iter(self) -> iter:
62
- return self._key_iter
63
-
64
- @key_iter.setter
65
- def key_iter(self, key_iter: iter) -> None:
66
- self._key_iter = key_iter
67
-
68
- @property
69
- def algorithm(self) -> str:
70
- return self._algorithm_name
71
-
72
- @algorithm.setter
73
- def algorithm(self, algorithm: str) -> None:
74
- self._algorithm_name = algorithm.lower()
75
- self._algorithm = self._set_algorithm()
76
-
77
- @property
78
- def character_replacements(self) -> dict:
79
- return self._char_replacements
80
-
81
- def _set_algorithm(self):
82
- match self._algorithm_name:
83
- case 'caesar':
84
- return CaesarCipher(self._shift)
85
- case 'affine':
86
- return AffineCipher(self._multiplier, self._shift)
87
- case 'playfair':
88
- return PlayfairCipher(self._key_str)
89
- case 'hill':
90
- return HillCipher(self._key_iter)
91
- case _:
92
- raise InvalidAlgorithmException(self._algorithm_name)
93
-
94
- def _update_algorithm_properties(self) -> None:
95
- self._algorithm = self._set_algorithm()
96
-
97
- def _custom_cipher(self, password: str) -> str:
98
- for char, replacement in self._char_replacements.items():
99
- password = password.replace(char, replacement)
100
- for i in range(len(password)):
101
- if password[i] in self._text:
102
- password = password.replace(password[i], password[i].upper())
103
- return password
104
-
105
- def replace_character(self, char: str, replacement: str) -> None:
106
- self._char_replacements[char[0]] = replacement
107
-
108
- def reset_character(self, char: str) -> None:
109
- if char in self._char_replacements:
110
- del self._char_replacements[char]
111
-
112
- def generate_raw_password(self) -> str:
113
- self._update_algorithm_properties()
114
- return self._algorithm.encrypt(self._text)
115
-
116
- def generate_password(self) -> str:
117
- return self._custom_cipher(self.generate_raw_password())
File without changes
File without changes