Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Kamesh Raj
Published © LGPL

SecuVault

SecuVault is a secure password management and generation tool that combines robust encryption, bcrypt hashing, and Fernet symmetric key.

ExpertFull instructions provided8 hours138
SecuVault

Things used in this project

Story

Read more

Code

SecuVault - Source Code

Python
import bcrypt
from cryptography.fernet import Fernet
import pyotp
import getpass
import json
import os

# Initialize encryption key
encryption_key = Fernet.generate_key()
cipher_suite = Fernet(encryption_key)

# Database to store user data
user_database = {}

def save_data():
    # Save user data to a file (insecure for production use)
    with open("user_data.json", "w") as file:
        json.dump(user_database, file)

def load_data():
    # Load user data from a file (insecure for production use)
    global user_database
    if os.path.exists("user_data.json"):
        with open("user_data.json", "r") as file:
            user_database = json.load(file)

def register_user():
    print("=== User Registration ===")
    username = input("Enter your username: ")
    
    # Check if username already exists
    if username in user_database:
        print("Username already exists. Please choose another.")
        return
    
    # Get a strong password from the user
    while True:
        password = getpass.getpass(prompt="Enter a strong password: ")
        
        # Password strength check
        if (
            any(c.isupper() for c in password)
            and any(c.islower() for c in password)
            and any(c.isdigit() for c in password)
            and any(c in "!@#$%^&*()-_+=<>,.?/:;{}[]|~" for c in password)
            and len(password) >= 8
        ):
            break
        else:
            print("Password is weak. Include uppercase, lowercase, digits, and special characters.")
    
    # Hash and store the password
    hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
    user_database[username] = {
        'password': hashed_password.decode('utf-8'),
        'services': {}
    }

    save_data()
    print("Registration successful.")

def login(username):
    print("=== User Login ===")
    
    # Check if username exists
    if username not in user_database:
        print("Username not found.")
        return False
    
    # Get the stored hashed password
    stored_password = user_database[username]['password']
    
    # Get password from user
    password = getpass.getpass(prompt="Enter your password: ")
    
    # Check password using bcrypt
    if bcrypt.checkpw(password.encode('utf-8'), stored_password.encode('utf-8')):
        print("Login successful.")
        return True
    else:
        print("Incorrect password.")
        return False

def generate_password():
    print("=== Password Generation ===")
    length = int(input("Enter the length of the password: "))
    
    # Generate a random password
    password = Fernet.generate_key().decode('utf-8')[:length]
    
    print(f"Generated Password: {password}")

def store_password(username):
    print("=== Store Password ===")
    if not login(username):
        return
    
    service = input("Enter the service name: ")
    password = getpass.getpass(prompt="Enter the password: ")
    
    # Encrypt the password before storing
    encrypted_password = cipher_suite.encrypt(password.encode('utf-8'))

    user_database[username]['services'][service] = encrypted_password
    save_data()
    print("Password stored successfully.")

def retrieve_password():
    print("=== Retrieve Password ===")
    username = input("Enter your username: ")
    if not login(username):
        return
    
    service = input("Enter the service name: ")
    
    if service in user_database[username]['services']:
        # Decrypt the stored password before displaying
        encrypted_password = user_database[username]['services'][service]
        decrypted_password = cipher_suite.decrypt(encrypted_password).decode('utf-8')
        
        print(f"Password for {service}: {decrypted_password}")
    else:
        print("Service not found.")

# Example usage
load_data()

while True:
    print("\n1. Register\n2. Login\n3. Generate Password\n4. Store Password\n5. Retrieve Password\n6. Exit")
    choice = input("Select an option: ")

    if choice == '1':
        register_user()
    elif choice == '2':
        username = input("Enter your username: ")
        login(username)
    elif choice == '3':
        generate_password()
    elif choice == '4':
        username = input("Enter your username: ")
        store_password(username)
    elif choice == '5':
        retrieve_password()
    elif choice == '6':
        break
    else:
        print("Invalid choice. Please try again.")

Credits

Kamesh Raj
4 projects • 1 follower
A driven and adaptable engineering student with business acumen, seeking to leverage my skills and passion for computer science.
Contact

Comments

Please log in or sign up to comment.