First working version (encryption only)

master
TitanE 10 months ago
parent cbb49a7047
commit 54618515e2

4
.gitignore vendored

@ -1,8 +1,6 @@
# Custom
*.dat
*.asc
*.asc.s
*.db
# ---> Python
# 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
import pickle
from math import log2
from password_strength import PasswordStats
from getpass import getpass as gp
from secrets import token_urlsafe
from random import randint as rint, SystemRandom as sr
from atexit import register
from gc import collect
from os import urandom, path, remove
from cryptography.fernet import Fernet, InvalidSignature, InvalidToken
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
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:
while True:
try:
import base64
import pickle
from pysqlcipher3 import dbapi2 as sc
from math import log2
from password_strength import PasswordStats
from getpass import getpass as gp
from secrets import token_urlsafe
from random import randint as rint, SystemRandom as sr
from atexit import register
from os import urandom, path, remove
except ModuleNotFoundError:
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 database_enc():
conn = sc.connect("database/keyvault.db")
cursor = conn.cursor()
if path.isfile("database/keyvault.db"):
for _ in range(3):
try:
db = pickle.load(f)
except EOFError:
print("\nDatabase loaded.")
password = gp(prompt = "Enter master password: ")
conn.execute(f"PRAGMA key = {password}")
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
shred()
def shred():
with open("database/db.dat", "wb") as f:
for _ in range(5):
f.seek(0)
f.write(urandom(path.getsize("database/db.dat")))
remove("database/db.dat")
def clearmem():
db = rint(100000000000000000000000000000000000000000000000000000000000, 999999999999999999999999999999999999999999999999999999999999)
db = None
except sc.DatabaseError:
print("Incorrect password.\n")
passCorrect = False
if not passCorrect:
print("You have entered a wrong password three times. Please restart the program to try again.")
exit()
else:
print("You have not setup a master password yet. Please set one below.\n")
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():
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)
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:
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():
password = gp(prompt = "\nEnter the password to check its strength (hidden for privacy!): ")
@ -117,27 +106,17 @@ def strength():
else:
print(f"[PASSWORD STRENGTH]: {passstrength}\n[PASSWORD ENTROPY]: {entropy} bits\nYour password is practically uncrackable.\n")
global db
register(clearmem)
global conn
global cursor
database_enc()
with open("files/strength/common-passwords.txt", "r") as f:
common_passwords = f.read()
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")
while True:
@ -150,3 +129,6 @@ while True:
gen()
elif command == 'strength':
strength()
elif command == 'exit':
print("Thank for you using keyvault!")
exit()

Loading…
Cancel
Save