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,93 +1,82 @@
import base64 try:
import pickle import base64
from math import log2 import pickle
from password_strength import PasswordStats from pysqlcipher3 import dbapi2 as sc
from getpass import getpass as gp from math import log2
from secrets import token_urlsafe from password_strength import PasswordStats
from random import randint as rint, SystemRandom as sr from getpass import getpass as gp
from atexit import register from secrets import token_urlsafe
from gc import collect from random import randint as rint, SystemRandom as sr
from os import urandom, path, remove from atexit import register
from cryptography.fernet import Fernet, InvalidSignature, InvalidToken from os import urandom, path, remove
from cryptography.hazmat.primitives import hashes except ModuleNotFoundError:
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC 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.")
exit()
def encrypt_db(password):
binpass = password.encode() def database_enc():
salt = urandom(16) conn = sc.connect("database/keyvault.db")
kdf = PBKDF2HMAC( cursor = conn.cursor()
algorithm=hashes.SHA512(), if path.isfile("database/keyvault.db"):
length=32, for _ in range(3):
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:
while True:
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():
while True: userpass = input("Type 'u' to generate usernames or 'p' for passwords: ").lower()
length = int(input("Enter password length (above 8 only): ")) if userpass == 'u':
if length <= 7: with open("files/generation/wordlist.txt", "r") as f:
print("The password is too short. Please enter it again.") words = f.readlines()
else: word1, word2 = rint(0, 8874), rint(0, 8874)
break username = f"{words[word1][0:-1]}{words[word2][0:-1]}{rint(0, 100000)}"
pool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~@#$%^&*()-_=+]}[{\"';:.>,</?" print(username)
strength = ''.join(sr().choice(pool) for i in range(length)) elif userpass == 'p':
print(strength) while True:
length = int(input("Enter password length (above 8 only): "))
if length <= 7:
print("The password is too short. Please enter it again.")
else:
break
pool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~@#$%^&*()-_=+]}[{\"';:.>,</?"
strength = ''.join(sr().choice(pool) for i in range(length))
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