7
7
8
8
9
9
class Secauax :
10
+ """
11
+ Secauax encryption class
12
+ """
13
+
10
14
def __init__ (self ):
15
+ """
16
+ Init method
17
+ """
11
18
self .key_ = Fernet .generate_key ()
12
19
13
20
def __str__ (self ):
@@ -23,7 +30,7 @@ def key(self):
23
30
@staticmethod
24
31
def load_key (path : Union [Path , str ]) -> bytes :
25
32
"""
26
- Return a key from a ". key" file .
33
+ Return a key from a file. This key must be valid, otherwise, an error will be thrown .
27
34
:param path: path to key
28
35
:return: bytes
29
36
"""
@@ -35,39 +42,42 @@ def load_key(path: Union[Path, str]) -> bytes:
35
42
36
43
def load_key_into_class (self , path : Union [Path , str ]):
37
44
"""
38
- Replace the key class variable with loaded key.
39
- File extension is ".key"
45
+ Replace the key class variable with the loaded key and return it.
40
46
:param path: path to key
41
47
:return: bytes
42
48
"""
43
49
self .key_ = Secauax .load_key (path )
44
50
return self .key
45
51
46
- def save_key (self , filename : Union [Path , str ]) -> None :
52
+ def save_key (self , filename : Union [Path , str ]) -> bool :
47
53
"""
48
- Save generated key to a file.
49
- File extension is ".key"
54
+ Save set key to a file. You can choose the file extension, although the ".key" extension is recommended .
55
+ This method returns a true boolean if the file is saved successfully. An OSError will result in a false boolean.
50
56
:param filename: path to save the key
51
- :return: None
57
+ :return: bool
52
58
"""
53
- with open (filename , 'wb' ) as filekey :
54
- filekey .write (self .key )
55
- filekey .close ()
59
+ try :
60
+ with open (filename , 'wb' ) as filekey :
61
+ filekey .write (self .key )
62
+ filekey .close ()
63
+ except OSError :
64
+ return False
65
+ return True
56
66
57
67
def encrypt_file (self , path : Union [Path , str ], filename : Union [Path , str ] = None ) -> bytes :
58
68
"""
59
- Encrypt a file with a generated key.
60
- DISCLAIMER: if no " filename" parameter is specified, the file will be overwritten .
61
- Returns the encrypted file data in bytes.
62
- :param path: path to original file
69
+ Encrypt a file with the set key.
70
+ Attention: If the filename parameter is not specified, the new file will overwrite the original .
71
+ It returns the encrypted file data in bytes.
72
+ :param path: path to the original file
63
73
:param filename: path to save the encrypted file
64
74
:return: bytes
65
75
"""
66
76
fernet = Fernet (self .key )
67
77
68
78
# Open original file
69
79
with open (path , "rb" ) as file :
70
- original_file = file .read ()
80
+ original_file = file .read () # Read data in bytes
71
81
file .close ()
72
82
73
83
# Encrypt the file
@@ -83,27 +93,32 @@ def encrypt_file(self, path: Union[Path, str], filename: Union[Path, str] = None
83
93
def bulk_encrypt (self ,
84
94
pathname : Union [Path , str ],
85
95
output_directory : Union [Path , str ] = None ,
86
- file_extension : str = "*" ) -> None :
96
+ file_extension : str = "*" ) -> bool :
87
97
"""
88
- Encrypt all files inside a directory and save them into another directory.
89
- If the "output_directory" is the same as the "pathname", the files will be overwritten.
98
+ Encrypt all the files inside a directory and save them into another directory.
99
+ Attention: If the output_directory parameter is not specified, the new file(s) will overwrite the original(s).
100
+ This method returns a true boolean if the files are saved successfully. Any error will result in a false boolean.
90
101
:param pathname: path to the decrypted folder
91
102
:param output_directory: path to save the encrypted files
92
- :param file_extension: select which files will be encrypted. Format : ".png" or, for all type of files: "*"
93
- :return: None
103
+ :param file_extension: filter files by extension : ".png" / ".txt" / ...
104
+ :return: bool
94
105
"""
95
106
96
107
assert os .path .isdir (pathname ), "pathname path doesn't exist"
97
108
assert os .path .isdir (output_directory ), "output_directory path doesn't exist"
98
109
99
- for file in glob .glob (os .path .join (pathname , file_extension )):
100
- self .encrypt_file (file , os .path .join (output_directory , os .path .basename (file )))
110
+ try :
111
+ for file in glob .glob (os .path .join (pathname , file_extension )):
112
+ self .encrypt_file (file , os .path .join (output_directory , os .path .basename (file )))
113
+ except :
114
+ return False
115
+ return True
101
116
102
117
def decrypt_file (self , path : Union [Path , str ], filename : Union [Path , str ] = None ) -> bytes :
103
118
"""
104
- Decrypt a file with a generated key.
105
- DISCLAIMER: if no " filename" parameter is specified, the file will be overwritten .
106
- Returns the decrypted file data in bytes.
119
+ Decrypt a file with the set key.
120
+ Attention: If the filename parameter is not specified, the new file will overwrite the original .
121
+ It returns the decrypted file data in bytes.
107
122
:param path: path to the encrypted file
108
123
:param filename: path to save the decrypted file
109
124
:return: bytes
@@ -126,18 +141,23 @@ def decrypt_file(self, path: Union[Path, str], filename: Union[Path, str] = None
126
141
def bulk_decrypt (self ,
127
142
pathname : Union [Path , str ],
128
143
output_directory : Union [Path , str ] = None ,
129
- file_extension : str = "*" ) -> None :
144
+ file_extension : str = "*" ) -> bool :
130
145
"""
131
- Decrypt all files inside a directory and save them into another directory.
132
- If the "output_directory" is the same as the "pathname", the files will be overwritten.
146
+ Decrypt all the files inside a directory and save them into another directory.
147
+ Attention: If the output_directory parameter is not specified, the new file(s) will overwrite the original(s).
148
+ This method returns a true boolean if the files are saved successfully. Any error will result in a false boolean.
133
149
:param pathname: path to encrypted folder
134
150
:param output_directory: path to save the decrypted files
135
151
:param file_extension: select which files will be decrypted. Format: ".png" or, for all type of files: "*"
136
- :return: None
152
+ :return: bool
137
153
"""
138
154
139
155
assert os .path .isdir (pathname ), "pathname path doesn't exist"
140
156
assert os .path .isdir (output_directory ), "output_directory path doesn't exist"
141
157
142
- for file in glob .glob (os .path .join (pathname , file_extension )):
143
- self .decrypt_file (file , os .path .join (output_directory , os .path .basename (file )))
158
+ try :
159
+ for file in glob .glob (os .path .join (pathname , file_extension )):
160
+ self .decrypt_file (file , os .path .join (output_directory , os .path .basename (file )))
161
+ except :
162
+ return False
163
+ return True
0 commit comments