· 5 years ago · Sep 21, 2020, 03:22 PM
1import base64
2import os
3import secrets
4from cryptography.fernet import Fernet
5from cryptography.hazmat.primitives import hashes
6from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
7from get_logs import LogsReturn
8from config import (salt as s,
9 secret_key as sk)
10
11class Encryption:
12 """
13 class which is dedicated to encryption or
14 decryption values for the database
15 ATTENTION: salt and key don't require to
16 be regenerated. It is necessary only for
17 test or if someone needs to change them
18 """
19 def __init__(self, salt=s):
20 self.__kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000,)
21 self.__key = base64.urlsafe_b64encode(self.__kdf.derive(sk))
22 self.encrypt_way = Fernet(self.__key)
23 self.return_logs = LogsReturn().return_logs_encryption
24
25
26 def encrypt(self, string_to_encrypt:str) -> str:
27 """
28 Method which is dedicated to a encrypting values
29 Input: string_to_encrypt = string which is going to be encrypted
30 Output: we have successfully encrypted our string
31 """
32 pwd_to_encrypt = string_to_encrypt.encode()
33 return self.encrypt_way.encrypt(pwd_to_encrypt).decode()
34
35
36 def decrypt(self, string_to_encrypt:str) -> str:
37 """
38 Method which is dedicated to a decrypting values
39 Input: string_to_encrypt = string which is required to be decrypted
40 Output: we have successfully decrypted our sting
41 """
42 en = string_to_encrypt.encode()
43 pwd_to_decrypt = self.encrypt_way.decrypt(en)
44 return pwd_to_decrypt.decode()
45
46
47 @staticmethod
48 def add_salt() -> str:
49 """
50 Method which is dedicated to adding salt for the encryption
51 proccess to make the string storage more secure
52 Input: None
53 Output: we have got salt, which helps us to encrypt the data
54 """
55 return os.urandom(16)
56
57
58 @staticmethod
59 def generate_key() -> str:
60 """
61 Method which is dedicated to a generating key for the string
62 Input: None
63 Output: we have successfully generated key value for the work
64 """
65 return Fernet.generate_key()
66
67
68 def test(self, string_check:str="Secret message!") -> None:
69 """
70 Method which is dedicated to test of the encode/decode system
71 Input: string_check = string for showing that
72 Output: True if we encrypted & decrypted and it's the same string
73 """
74 encryption = self.encrypt(string_check)
75 val = bool(string_check==self.decrypt(encryption))
76 if val:
77 print(self.return_logs(1, [string_check]))
78 else:
79 print(self.return_logs(2, [string_check]))
80
81
82 def compare_pwd(self, pwd_db:str, pwd_used:str) -> bool:
83 """
84 Method which is dedicated to a comparisson
85 Input: pwd_db = password from database
86 pwd_used = password input from values
87 Output: boolean value which signify does they are identical
88 """
89 print(secrets.compare_digest(pwd_db, pwd_used))
90
91
92
93if __name__ == "__main__":
94 encryption_class = Encryption()
95 encryption_class.compare_pwd(b'gAAAAABfaLNdhPvd65S_ipQKtXi09E-qonKwNBammx5zCxobQ6GQeyYHgPNiPxei0m3a7giz77wvnhnU-bZoC9wcZZUxT23dKw==',
96b'gAAAAABfaJFkfpFSvaf-r6TPeFw5-z2tNjtrVQqTQgJBcByf139Rmk0JC3lL98tUDiOBl0UnkOlDWNbWXBJub4viqOCVh7f_lw==')