-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
83 lines (66 loc) · 3.35 KB
/
cipher.py
File metadata and controls
83 lines (66 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Cipher:
'''
A representation of a cipher, allowing to add custom ciphers to the
program.
Ciphers can be one-way (encrypting only, like SHA hashes), or two-way
(encrypting and decrypting) type.
You can create your custom cipher(-s) by creating a Python file in
the 'ciphers' directory and defining class(-es) which inherit the
'Cipher' class. In your child class you must define two specific
methods:
encrypt(self, str, encoding) -> str —— the encryption method;
decrypt(self, str, encoding) -> str —— the decryption method.
By default, UTF-8 encoding is used.
As an example, you can refer to the standard 'base64.py' inplemen-
tation.
:attrib 'categories': dict<str, Action>
List of ciphers categories you can pick while initializing a Cipher
instance
'''
categories = {'hash': Action(QApplication.translate('CipherCategories', 'Hashing algorithm')),
'simple': Action(QApplication.translate('CipherCategories', 'Simple cryptography')),
'btte': Action(QApplication.translate('CipherCategories', 'Binary-to-text encoding'))}
def __init__(self, type: bool, displayName: str, origin: str,
category: str in categories.keys(), version: tuple[int] = (1, 0)):
'''
Initialize a cipher instance with the following parameters:
:param 'type': bool
If True, the cipher expects to have both 'encrypt' and
'decrypt" methods, so it is a two-way one; if False,
the cipher expects to have 'encrypt' method only.
:param 'displayName': str
The cipher's name that is going to be shown in the ciphers'
selection combo box.
:param 'origin': str
Name of the origin file. While creating a child class,
send the '__name__' property to this argumen
:kwparam 'category': str
Name of the category the cipher belongs to. For example,
there are hashes, simple cryptography methods like magic
square, etc. Available categories are:
'hash' —— Hashing algorithms;
'simple' —— Simple cryptography;
'btte' —— Binary-to-text encoding.
:kwparam 'version': tuple<int, int>
Version of the cipher
'''
self.type, self.displayName, self.origin, self.category, self.version = (
type, displayName, f'{origin[8:]}.py', category, version)
def getInformation(self, root: Window) -> str:
'''
Get a string representing cipher's properties to show it in
the UI.
:returns: str
'''
translatedStrings = (
root.translate('CipherInfo', 'Category'),
root.translate('CipherInfo', 'Type'),
root.translate('CipherInfo', 'one-way'),
root.translate('CipherInfo', 'two-way'),
root.translate('CipherInfo', 'Defined in'),
root.translate('CipherInfo', 'Version'))
return (
f'{translatedStrings[0]}: {self.categories[self.category].text()}\n'
f'{translatedStrings[1]}: {(translatedStrings[3] if self.type else translatedStrings[2])}\n'
f'{translatedStrings[4]}: {self.origin}\n'
f'{translatedStrings[5]}: {self.version[0]}.{self.version[1]}')