-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabaseConnection.py
More file actions
75 lines (64 loc) · 2.1 KB
/
Copy pathdatabaseConnection.py
File metadata and controls
75 lines (64 loc) · 2.1 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
import sqlite3
from sqlite3 import Error
from cryptography.fernet import Fernet
from hashlib import sha256
class Criptography:
def __init__(self, file=''):
self.fileKey = file
@staticmethod
def create_key(file):
key = Fernet.generate_key()
with open(f'{file}.txt', 'wb') as file_key:
file_key.write(key)
@staticmethod
def load_key(file):
key = None
with open(f'{file}.txt', 'r') as file_key:
key = file_key.read()
return key.encode()
def crypt(self, message, type_encode='register', type_cryptography='fernet'):
match type_cryptography:
case 'fernet':
key = Fernet(self.load_key(self.fileKey))
# coding message
message_code = key.encrypt(message.encode())
match type_encode:
case 'register':
return message_code
case 'search':
return "b'"
case 'hash':
hash_password = sha256(message.encode())
return hash_password.hexdigest()
def decode(self, message):
key = Fernet(self.load_key(self.fileKey))
# decoding message
message_decode = key.decrypt(message)
return message_decode.decode('utf-8')
class DataBase:
def __init__(self, file):
self.file = file
def conectionDatabase(self):
conect = None
try:
conect = sqlite3.connect(self.file)
except Error as er:
return print(er)
finally:
return conect
def searchDatabase(self, query):
conect = self.conectionDatabase()
cursor = conect.cursor()
cursor.execute(query)
res = cursor.fetchall()
conect.close()
return res
def crud(self, sql, execute=0, valores=None):
conect = self.conectionDatabase()
cursor = conect.cursor()
if execute == 0:
cursor.execute(sql)
elif execute == 1:
cursor.executemany(sql, valores)
conect.commit()
conect.close()