TermPY/packages/vault.py

105 lines
2.8 KiB
Python
Raw Normal View History

2023-09-21 23:57:03 +00:00
# ["vault", "packages.vault", ["vault"]]
# Made By OusmBlueNinja
import getpass
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
import base64
2023-09-21 23:57:03 +00:00
# Global variables
2023-09-21 23:57:03 +00:00
key = None
passwords = []
2023-09-21 23:57:03 +00:00
def initialize_vault():
global key
2023-09-21 23:57:03 +00:00
print("Welcome to the Vault!")
master_password = getpass.getpass("Enter your master password: ")
salt = b'some_salt_here' # Replace with a secure salt value
2023-09-21 23:57:03 +00:00
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
iterations=100000, # Adjust the number of iterations as needed for security
salt=salt,
length=32 # Use a fixed key length (256 bits)
)
2023-09-21 23:57:03 +00:00
key = base64.urlsafe_b64encode(kdf.derive(master_password.encode()))
print("Vault initialized successfully.")
2023-09-21 23:57:03 +00:00
def save_vault():
global key, passwords
if key is None:
print("You must log in first to access the vault.")
2023-09-21 23:57:03 +00:00
return
cipher_suite = Fernet(key)
encrypted_passwords = [cipher_suite.encrypt(password.encode()) for password in passwords]
2023-09-21 23:57:03 +00:00
with open("vault.txt", "wb") as file:
for encrypted_password in encrypted_passwords:
file.write(encrypted_password + b'\n')
2023-09-21 23:57:03 +00:00
def load_vault():
global key, passwords
if key is None:
print("You must log in first to access the vault.")
return
2023-09-21 23:57:03 +00:00
try:
with open("vault.txt", "rb") as file:
lines = file.read().splitlines()
cipher_suite = Fernet(key)
passwords = [cipher_suite.decrypt(line).decode() for line in lines]
except FileNotFoundError:
passwords = []
def vault_add(password):
global key, passwords
if key is None:
print("You must log in first to access the vault.")
return
2023-09-21 23:57:03 +00:00
passwords.append(password)
print("Password added to the vault.")
2023-09-21 23:57:03 +00:00
def vault_retrieve():
global key, passwords
if key is None:
print("You must log in first to access the vault.")
return
2023-09-21 23:57:03 +00:00
for i, password in enumerate(passwords):
print(f"Password {i + 1}: {password}")
2023-09-21 23:57:03 +00:00
def vault(command: list):
if len(command) < 1:
print("Usage: vault [login|add|retrieve|save|load]")
return
2023-09-21 23:57:03 +00:00
action = command[0].lower()
2023-09-21 23:57:03 +00:00
if action == "login":
initialize_vault()
elif action == "add" and len(command) == 2:
password = command[1]
vault_add(password)
elif action == "retrieve":
vault_retrieve()
elif action == "save":
save_vault()
print("Vault saved.")
elif action == "load":
load_vault()
print("Vault loaded.")
2023-09-21 23:57:03 +00:00
else:
print("Invalid action or arguments. Usage: vault [login|add|retrieve|save|load]")
# Example usage:
# vault ["login"]
# vault ["add", "secretpassword"]
# vault ["retrieve"]
# vault ["save"]
# vault ["load"]