add base files
This commit is contained in:
@@ -4,4 +4,7 @@ version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.13"
|
||||
dependencies = []
|
||||
dependencies = [
|
||||
"cryptography>=44.0.2",
|
||||
"playwright>=1.50.0",
|
||||
]
|
||||
|
||||
32
src/database.py
Normal file
32
src/database.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import sqlite3
|
||||
|
||||
|
||||
class Database:
|
||||
def __init__(self):
|
||||
self.conn = sqlite3.connect("database.db")
|
||||
self.cursor = self.conn.cursor()
|
||||
self.create_table()
|
||||
|
||||
def create_table(self):
|
||||
self.cursor.execute(
|
||||
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, password TEXT)"
|
||||
)
|
||||
self.conn.commit()
|
||||
|
||||
def add_user(self, username, password):
|
||||
self.cursor.execute(
|
||||
"INSERT INTO users (username, password) VALUES (?, ?)", (username, password)
|
||||
)
|
||||
self.conn.commit()
|
||||
|
||||
def get_password(self, username):
|
||||
self.cursor.execute(
|
||||
"SELECT password FROM users WHERE username = ?", (username,)
|
||||
)
|
||||
return self.cursor.fetchone()
|
||||
|
||||
def change_password(self, username, password):
|
||||
self.cursor.execute(
|
||||
"UPDATE users SET password = ? WHERE username = ?", (password, username)
|
||||
)
|
||||
self.conn.commit()
|
||||
36
src/security.py
Normal file
36
src/security.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import cryptography
|
||||
from cryptography.fernet import Fernet
|
||||
from .database import Database
|
||||
|
||||
# import library to create a random key
|
||||
import random
|
||||
|
||||
|
||||
def set_data():
|
||||
user = input("Enter username: ")
|
||||
fernet_key = Fernet.generate_key()
|
||||
with open("key.key", "w") as key_file:
|
||||
key = f"{user}|{fernet_key.decode()}"
|
||||
key_file.write(key)
|
||||
|
||||
# ask for password, do not show the password
|
||||
password = input("Enter password: ")
|
||||
# encrypt the password
|
||||
cipher_suite = Fernet(fernet_key)
|
||||
ciphered_text = cipher_suite.encrypt(password.encode())
|
||||
db = Database()
|
||||
db.add_user(user, ciphered_text)
|
||||
|
||||
|
||||
def get_data():
|
||||
with open("key.key", "r") as key_file:
|
||||
key = key_file.read()
|
||||
|
||||
user = key.split("|")[0]
|
||||
key = key.split("|")[1]
|
||||
db = Database()
|
||||
password = db.get_password(user)
|
||||
cipher_suite = Fernet(key)
|
||||
# decrypt the password
|
||||
plain_text = cipher_suite.decrypt(password[0])
|
||||
print(plain_text.decode())
|
||||
Reference in New Issue
Block a user