@ -1,84 +1,71 @@
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 )
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 \n 1. Open the terminal (Linux/MacOS) or command prompt (Windows). \n 2. Navigate to this directory and then to the files directory. \n 3. Type ' pip install -r dependencies.txt ' . \n 4. Restart the program. \n \n If you have followed all the steps correctly, keyvault will work on the next start. " )
exit ( )
with open ( " database/db.dat " , " rb " ) as f :
while True :
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 ( " \n Database 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 ( )
except sc . DatabaseError :
print ( " Incorrect password. \n " )
passCorrect = False
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 " )
if not passCorrect :
print ( " You have entered a wrong password three times. Please restart the program to try again. " )
exit ( )
def clearmem ( ) :
db = rint ( 100000000000000000000000000000000000000000000000000000000000 , 999999999999999999999999999999999999999999999999999999999999 )
db = None
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 ( " \n The 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 ( ) :
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 :
@ -88,6 +75,8 @@ def gen():
pool = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~@#$ % ^&*()-_=+]}[ { \" ' ;:.>,</? "
strength = ' ' . join ( sr ( ) . choice ( pool ) for i in range ( length ) )
print ( strength )
else :
print ( " Incorrect input. " )
def strength ( ) :
password = gp ( prompt = " \n Enter 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 \n Your 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 ( " \n keyvault initialized. " )
for _ in range ( 4 ) :
try :
if _ != 3 :
password = gp ( prompt = " \n Enter 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 ( )