Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Session 03 - Python for Forensics

The code here is to support the Session 03 lecture.

passwordmanager.py

import sys
import random
import string
import pyperclip  # You might need to install this library using pip if it's not already installed

def genRandPassword(length=12):
    characters = string.ascii_letters + string.digits + string.punctuation
    return ''.join(random.choice(characters) for _ in range(length))

def savePassword(service, password):
    with open('passwords.txt', 'a') as file:
        file.write(f"{service}: {password}\n")

def main():
    print("Welcome to the Password Manager!")

    service = input("Enter the name of the service (e.g., email, website, bank): ")
    userChoice = input("Do you want to generate a random password? (yes/no): ")

    if userChoice.lower() == 'yes':
        password = genRandPassword()
    else:
        password = input("Enter your password: ")

    savePassword(service, password)
    print("Password saved successfully!")

    copyToClipboard = input("Do you want to copy the password to the clipboard? (yes/no): ")

    if copyToClipboard.lower() == 'yes':
        pyperclip.copy(password)
        print("Password copied to clipboard.")

if __name__ == "__main__":
    main()
[ Last updated: Tuesday 13 February 2024 @ 11:22:49 +0000 // branch: master // commit: bd41815 ]