First working version (encryption only)

master
TitanE 10 months ago
parent cbb49a7047
commit 54618515e2

4
.gitignore vendored

@ -1,8 +1,6 @@
# Custom # Custom
*.dat *.db
*.asc
*.asc.s
# ---> Python # ---> Python
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files

@ -0,0 +1,4 @@
pysqlcipher3
password_strength
hashlib
pyotp

File diff suppressed because it is too large Load Diff

@ -1,84 +1,71 @@
try:
import base64 import base64
import pickle import pickle
from pysqlcipher3 import dbapi2 as sc
from math import log2 from math import log2
from password_strength import PasswordStats from password_strength import PasswordStats
from getpass import getpass as gp from getpass import getpass as gp
from secrets import token_urlsafe from secrets import token_urlsafe
from random import randint as rint, SystemRandom as sr from random import randint as rint, SystemRandom as sr
from atexit import register from atexit import register
from gc import collect
from os import urandom, path, remove from os import urandom, path, remove
from cryptography.fernet import Fernet, InvalidSignature, InvalidToken except ModuleNotFoundError:
from cryptography.hazmat.primitives import hashes print("You have not installed the required modules. Follow these steps to do so:\n\n1. Open the terminal (Linux/MacOS) or command prompt (Windows).\n2. Navigate to this directory and then to the files directory.\n3. Type 'pip install -r dependencies.txt'.\n4. Restart the program.\n\nIf you have followed all the steps correctly, keyvault will work on the next start.")
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC exit()
def encrypt_db(password):
binpass = password.encode()
salt = urandom(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA512(),
length=32,
salt=salt,
iterations=1500000,
)
key = base64.urlsafe_b64encode(kdf.derive(binpass))
fernet = Fernet(key)
with open("database/db.dat", "rb") as f:
data = f.read()
encr = fernet.encrypt(data)
with open("database/db.asc", "wb") as f:
f.write(encr)
with open("database/db.s", "wb") as f:
f.write(salt)
def decrypt_db(password):
with open("database/db.s", "rb") as f:
salt = f.read()
binpass = password.encode()
kdf = PBKDF2HMAC(
algorithm=hashes.SHA512(),
length=32,
salt=salt,
iterations=1500000,
)
key = base64.urlsafe_b64encode(kdf.derive(binpass))
fernet = Fernet(key)
with open("database/db.asc", "rb") as f:
encrypted_data = f.read()
decr = fernet.decrypt(encrypted_data)
with open("database/db.dat", "wb") as f:
f.write(decr)
with open("database/db.dat", "rb") as f: def database_enc():
while True: conn = sc.connect("database/keyvault.db")
cursor = conn.cursor()
if path.isfile("database/keyvault.db"):
for _ in range(3):
try: try:
db = pickle.load(f) password = gp(prompt = "Enter master password: ")
except EOFError: conn.execute(f"PRAGMA key = {password}")
print("\nDatabase loaded.") conn.execute('''
CREATE TABLE IF NOT EXISTS data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
service TEXT,
username TEXT,
email TEXT,
password TEXT,
website TEXT,
category TEXT,
notes TEXT,
totp TEXT )
''')
passCorrect = True
break break
shred() except sc.DatabaseError:
print("Incorrect password.\n")
passCorrect = False
def shred(): if not passCorrect:
with open("database/db.dat", "wb") as f: print("You have entered a wrong password three times. Please restart the program to try again.")
for _ in range(5): exit()
f.seek(0)
f.write(urandom(path.getsize("database/db.dat")))
remove("database/db.dat")
def clearmem(): else:
db = rint(100000000000000000000000000000000000000000000000000000000000, 999999999999999999999999999999999999999999999999999999999999) print("You have not setup a master password yet. Please set one below.\n")
db = None while True:
mp, mp2 = gp(prompt = "Enter a secure master password (hidden for privacy!): "), gp(prompt = "Please enter it again: ")
if mp == mp2:
if len(mp) < 8:
print("\nThe master password you have set is too weak. Please set another one.")
else:
break
else:
print("Both of the passwords are different. Please enter the same password.")
conn.execute(f"PRAGMA key = {mp}")
conn.commit()
def gen(): def gen():
userpass = input("Type 'u' to generate usernames or 'p' for passwords: ").lower()
if userpass == 'u':
with open("files/generation/wordlist.txt", "r") as f:
words = f.readlines()
word1, word2 = rint(0, 8874), rint(0, 8874)
username = f"{words[word1][0:-1]}{words[word2][0:-1]}{rint(0, 100000)}"
print(username)
elif userpass == 'p':
while True: while True:
length = int(input("Enter password length (above 8 only): ")) length = int(input("Enter password length (above 8 only): "))
if length <= 7: if length <= 7:
@ -88,6 +75,8 @@ def gen():
pool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~@#$%^&*()-_=+]}[{\"';:.>,</?" pool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~@#$%^&*()-_=+]}[{\"';:.>,</?"
strength = ''.join(sr().choice(pool) for i in range(length)) strength = ''.join(sr().choice(pool) for i in range(length))
print(strength) print(strength)
else:
print("Incorrect input.")
def strength(): def strength():
password = gp(prompt = "\nEnter the password to check its strength (hidden for privacy!): ") password = gp(prompt = "\nEnter the password to check its strength (hidden for privacy!): ")
@ -117,27 +106,17 @@ def strength():
else: else:
print(f"[PASSWORD STRENGTH]: {passstrength}\n[PASSWORD ENTROPY]: {entropy} bits\nYour password is practically uncrackable.\n") print(f"[PASSWORD STRENGTH]: {passstrength}\n[PASSWORD ENTROPY]: {entropy} bits\nYour password is practically uncrackable.\n")
global db global db
register(clearmem) global conn
global cursor
database_enc()
with open("files/strength/common-passwords.txt", "r") as f: with open("files/strength/common-passwords.txt", "r") as f:
common_passwords = f.read() common_passwords = f.read()
print("\nkeyvault initialized.") print("\nkeyvault initialized.")
for _ in range(4):
try:
if _ != 3:
password = gp(prompt = "\nEnter your password (hidden for privacy!): ")
decrypt_db(password)
break
else:
print(" You have exceeded the maximum number of tries.")
exit()
except (InvalidSignature, InvalidToken):
print("Incorrect password.", end = '')
print("keyvault is ready to use! Type 'help' for a list of commands.\n") print("keyvault is ready to use! Type 'help' for a list of commands.\n")
while True: while True:
@ -150,3 +129,6 @@ while True:
gen() gen()
elif command == 'strength': elif command == 'strength':
strength() strength()
elif command == 'exit':
print("Thank for you using keyvault!")
exit()

Loading…
Cancel
Save