-
Notifications
You must be signed in to change notification settings - Fork 65
/
FernetDemo.py
70 lines (56 loc) · 1.93 KB
/
FernetDemo.py
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
import os
import cryptography
import base64
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
class FernetDemo(object):
__Content = None
def __init__(self, File, Key):
self.File = File
self.Key = Key.encode('utf-8')
self.KDF = PBKDF2HMAC(
algorithm = hashes.SHA256(),
length = 32,
salt = 'MyCustomSalt'.encode('utf-8'),
iterations = 100000,
backend = default_backend()
)
self.encodedKey = base64.urlsafe_b64encode(self.KDF.derive(self.Key))
self.fern = Fernet(self.encodedKey)
if not os.path.isfile(self.File):
with open(self.File,'w') as FileToEncrypt:
pass
@property
def Content(self):
return self.__Content
@property
def FileContent(self):
with open(self.File,'rb') as DecryptedFile:
return self.fern.decrypt(DecryptedFile.read()).decode('utf-8')
def unlockFile(self):
if not self.__Content is None:
print("The file is already unlocked!")
return
with open(self.File,'rb') as EncryptedFile:
self.__Content = self.fern.decrypt(EncryptedFile.read()).decode('utf-8')
print('#Content')
print(self.__Content)
print('#Content')
def addContent(self,ContentToAdd ):
self.__Content += ContentToAdd
with open(self.File,"wb") as EncryptedFile:
EncryptedFile.write(self.fern.encrypt(self.__Content.encode('utf-8')))
def lockFile(self):
if self.__Content is None:
print("There is nothing to add!")
return
with open(self.File,"wb") as EncryptedFile:
EncryptedFile.write(self.fern.encrypt(self.__Content.encode('utf-8')))
FD = FernetDemo(File = 'TestFile.txt',Key='Password123')
FD.unlockFile()
FD.addContent('THis is my first ever encrypted Content!')
FD.addContent('THis is my first ever encrypted Content!')
FD.lockFile()
print(FD.FileContent)