Skip to main content
Rollback to Revision 3
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127

The only non-standard python library is pycryptodome found here. Do NOT use the deprecated pycrypto package.

# STANDARD PYTHON MODULES
import os
import sys
import time
import struct
import traceback
from hashlib import sha512
from hashlib import blake2b as blake
from hashlib import sha3_512 as sha3
from hashlib import pbkdf2_hmac as pbkdf
from hashlib import shake_256 as shake256
from base64 import b64encode, b64decode
from json import loads as json_loads
from json import dumps as json_dumps
from subprocess import Popen, PIPE
from random import seed, randint
from binascii import hexlify
from getpass import getpass
from pprint import pprint

# THIRD PARTY MODULES
from Crypto import Random
from Crypto.Cipher import AES


# USER DEFINED SECURITY CONSTANTS
# WARNING: when you change these parameters your CypherVault will need to be deleted
MEGABYTES = 400
ITERATIONS = 1000000

# CURRENT RELEASE ID
VERSION = 0.00000003


def it(style, text):
    """
    colored text in terminal
    """
    emphasis = {
        "red": 91,
        "green": 92,
        "yellow": 93,
        "blue": 94,
        "purple": 95,
        "cyan": 96,
    }
    return ("\033[%sm" % emphasis[style]) + str(text) + "\033[0m"


def trace():
    """
    Stack trace report upon exception
    """
    return "\n\n" + str(time.ctime()) + "\n\n" + str(traceback.format_exc()) + "\n\n"


def doc_write(document, text):
    """
    write a dictionary to file
    """
    with open(document, "w+") as handle:
        handle.write(text)
        handle.close()


def doc_read(document):
    """
    read dictionary from file
    """
    with open(document, "r") as handle:
        text = handle.read()
        handle.close()
        return text


def clip_get():
    """
    read from clipboard
    """
    clip = Popen(["xclip", "-selection", "clipboard", "-o"], stdout=PIPE)
    clip.wait()
    return clip.stdout.read().decode()


def clip_set(data):
    """
    write to clipboard
    """
    clip = Popen(["xclip", "-selection", "clipboard"], stdin=PIPE)
    clip.stdin.write(data.encode())
    clip.stdin.close()
    clip.wait()


def crypto_pad(msg):
    """
    pad if length is not a multiple of 128 else unpad as required
    """
    return (
        (msg + (128 - len(msg) % 128) * chr(128 - len(msg) % 128))
        if len(msg) % 128
        else msg[0 : -msg[-1]]
    )


def crypto_100():
    """
    cryptographically secure 100 digit string formatted integer generator
    """
    str_random = ""
    while len(str_random) != 100:
        set_random = struct.unpack("QQQQQQ", os.urandom(48))
        str_random = ""
        for integer in set_random:
            str_random += str(integer)
        str_random = str(int(str_random[-100:]))
    return str_random


def crypto_wacky_digest(msg_digest, salt):
    """
    never roll your own... except as a backup plan and fun learning experience!
    random amount of multiple hashing types to impose novelty restraint
    """
    shaken_salt = shake256(salt.encode()).digest(16)
    # randomized iteration count
    for _ in range(int(salt[-4:])):
        # for each up to 10 iterations; 1 in 10 no skip
        for _ in range(int(salt[-1])):
            # salted 512 chacha stream cipher with permuted input block copy
            msg_digest = hexlify(blake(msg_digest, salt=shaken_salt).digest())
        for _ in range(int(salt[-2])):
            # keccak 512 sponge construction
            msg_digest = sha3(msg_digest).digest()
        for _ in range(int(salt[-3])):
            # standard sha512
            msg_digest = sha512(msg_digest).digest()

    return msg_digest


def crypto_digest(password):
    """
    iterative rounds of hashing to impose time restraint
    expanded password length to impose memory restraint
    """
    # any scheme which uses more than a few hundred MB of RAM
    # is almost certainly inefficient for GPU or FPGA implementations
    password *= max(1, int(MEGABYTES * 10 ** 6 / len(password)))
    # shake and digest the salt to 16 bytes
    salt = doc_read("cyphervault.txt").split("$", 1)[0]
    shaken_salt = shake256(salt.encode()).digest(16)
    # many iterations of salted 512 password based key derivation function (PBKDF)
    msg_digest = hexlify(pbkdf("sha512", password.encode(), shaken_salt, ITERATIONS))
    # multiple types of hashing to impose novelty restraint
    msg_digest = crypto_wacky_digest(msg_digest, salt)
    # final format to sha256 for 32 byte output
    return shake256(msg_digest).digest(32)


def crypto_cypher(vector, password):
    """
    AES encryption method in CBC mode
    """
    return AES.new(crypto_digest(password), AES.MODE_CBC, vector, segment_size=256)


def crypto_encrypt(message, password):
    """
    encryption routine
    """
    vector = Random.new().read(AES.block_size)
    cypher = crypto_cypher(vector, password)
    recursion = cypher.encrypt(crypto_pad(message))
    return b64encode(vector + recursion)


def crypto_decrypt(cyphertext, password):
    """
    decryption routine
    """
    vector = b64decode(cyphertext)
    cypher = crypto_cypher(vector[:16], password)
    recursion = cypher.decrypt(vector[16:])
    return crypto_pad(recursion).decode()


def crypto_indite(passwords):
    """
    \nencrypting the CypherVault...
    """
    print(it("purple", crypto_indite.__doc__))
    cyphersalt = crypto_100()
    cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
    doc_write(
        "cyphervault.txt", cyphersalt + "$" + cyphervault,
    )
    cyphervault = crypto_encrypt(
        json_dumps(passwords), passwords["master"]["master"]
    ).decode()
    doc_write(
        "cyphervault.txt", cyphersalt + "$" + cyphervault,
    )


def wallet_main(master=None, passwords=None):
    """
    ******************************************
    *** Welcome to CypherVault v"""
    msg = "***\n    ******************************************\n"
    print("\033c", it("green", wallet_main.__doc__ + ("%.8f " % VERSION) + msg))
    if passwords is None:
        passwords = wallet_initialize(master)
    crypto_indite(passwords)
    choice = wallet_choices(passwords["master"]["master"], passwords)
    if choice == 0:
        option_get(passwords)
    elif choice == 1:
        option_post(passwords)
    elif choice == 2:
        option_delete(passwords)
    elif choice == 3:
        option_suggest(passwords)
    elif choice == 4:
        option_print(passwords)
    elif choice == 5:
        option_print_full(passwords)
    elif choice == 6:
        crypto_indite(passwords)
        sys.exit()


def wallet_initialize(master):
    """
    initialize password dictionary and prompt for master password
    """
    # read password dictionary if none exists create a new encrypted cyphervault
    # all passwords are in format >>> passwords[site][user]
    try:
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        assert len(cyphervault) > 0
    except Exception:
        print("\nCypherVault not found, intitializing new...")
        doc_write("cyphervault.txt", (crypto_100() + "$n"))
        passwords = {"master": {"master": "password"}}
        crypto_indite(passwords)
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        print(it("purple", "\nyour default password is has been set to:"))
        print(it("green", "\npassword\n"))
    if master is None:
        master = getpass("Enter your master password:  ")
    # attempt to decrypt the cyphervault with the supplied password
    decrypted = False
    try:
        passwords = json_loads(crypto_decrypt(cyphervault, master))
        decrypted = True
    except Exception:
        trace()
        print(it("green", "\ninvalid master password, press Enter to try again..."))
        input("\npress Enter to return to main menu")
        wallet_main()  # recursion
    if decrypted:
        # after every successful login create a new salt
        crypto_indite(passwords)
        print(it("green", "\n    login successful!"))
        # warn if password is default
        if master == "password":
            print(it("purple", "\nyou should change default password immediately!"))
            print("\nyour master password is: ", it("green", master))
        # perform some tests on the password
        audit(master)

        return passwords


def wallet_choices(master, passwords):
    """
    1: ENTER A NEW PASSWORD OR EDIT A PASSWORD
    2: DELETE A PASSWORD
    3: SUGGEST A PASSWORD
    4: PRINT SITE/USER LIST
    5: PRINT SITE/USER/PASSWORD LIST
    6: EXIT
    """
    print(it("green", wallet_choices.__doc__))
    choice = input("input choice or press Enter to GET A PASSWORD: ")
    if not choice:
        choice = 0
    try:
        choice = int(choice)
        assert 0 <= choice <= 6
    except Exception:
        print("\033cinvalid choice (", choice, ") try again")
        time.sleep(2)
        wallet_main(master, passwords)
    return choice


def option_get(passwords):
    """
    the password has been copied to the clipboard
    \nyou only have 10 seconds to paste it via ctrl+V
    """
    site, user = input_site_user()
    found = False
    if site in passwords.keys():
        if user in passwords[site].keys():
            found = True
            clip_set(passwords[site][user])
            print(it("purple", option_get.__doc__))
            time.sleep(10)
            clip_set("")
            print("clipboard has been cleared")
            time.sleep(2)
    if not found:
        print("\nsite/user not found in wallet")
        response = input("\nwould you like to add this site/user? (y/n):  ")
        if response in ["", "y", "Y"]:
            option_post(passwords, site, user)
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_post(passwords, site=None, user=None):
    """
    post a new or updated password to the cyphervault
    """

    def update(passwords, site, user):

        print(it("purple", "\nsite/user:"), site, user, "\n")
        # double Enter the new password
        new_pass = getpass("\ninput password: ")
        if new_pass == getpass("\ninput new password again: "):
            audit(new_pass)
            if site not in passwords.keys():
                passwords[site] = {}
            passwords[site][user] = new_pass
            crypto_indite(passwords)
            print(it("green", "\nCypherVault has been updated"))
        else:  # recursion
            print(it("purple", "\npasswords do not match, try again..."))
            time.sleep(2)
            update(passwords, site, user)

    if site is None:
        site, user = input_site_user()
    create_new = True
    # update a password if it already exists
    if site in passwords.keys():
        if user in passwords[site].keys():
            print("\nsite:", site, "\nuser:", user)
            print(it("purple", "\nWARN: site/user already exists"))
            response = input("\nwould you like to overwrite this site/user? (y/n):")
            if response not in ["", "y", "Y"]:
                create_new = False
    if create_new:
        update(passwords, site, user)
    # return to main menu
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_delete(passwords):
    """
    remove a user from the cyphervault
    """
    print("\nEnter the site and user you would like to delete")
    site, user = input_site_user()
    if site != "master":
        found = False
        # check if the site/user exists in the passwords
        if site in passwords.keys():
            if user in passwords[site].keys():
                found = True
        if found:
            # remove the user
            del passwords[site][user]
            if passwords[site] == {}:
                # if there are no other users at that site, remove the site as well
                del passwords[site]
                crypto_indite(passwords)
                print(it("purple", "\nsite/user has been deleted"))
                time.sleep(2)
        else:
            # return to the main menu
            print(it("purple", "\nsite/user was not found"))

    else:
        print(it("purple", "you cannot delete the master password!"))
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_print(passwords):
    """
    print all site/user combinations in the cyphervault
    """
    print("")
    for site, logins in passwords.items():
        for user, _ in logins.items():
            print(it("green", site + " : " + user))
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_print_full(passwords):
    """
    \n\n
    WARNING: YOU ARE ABOUT TO EXPOSE YOUR UNENCRYPTED CYPHERVAULT
    Make sure you are in a private location!\n
    WARNING: DO NOT PRINT THIS TO PAPER
    CypherVault contents will be exposed on your printer hard drive\n
    WARNING: DO NOT SAVE THIS TO FILE
    CypherVault contents will be exposed on your local hard drive\n
    USE THIS FUNCTION ONLY TO INSPECT OR COPY - BY HAND - TO PAPER\n\n
    """
    print("\033c", it("purple", option_print_full.__doc__))
    msg = "\nare you sure you want to print your unencrypted CyperVault (y/n):  "
    response = input(it("green", msg))
    if response in ["y", "Y"]:
        msg = "\npress Enter to expose the CypherVault\npress Enter again to exit\n"
        print(it("green", msg))
        input()
        print("\033c\n\n\n")
        pprint(passwords)
    input(it("green", "\npress Enter to return to main menu"))
    wallet_main(passwords["master"]["master"], passwords)


def option_suggest(passwords, length=10):
    """
    press Enter to suggest another secure random password
    or any number 10 to 500, then Enter to change the length
    or any other key, then Enter to return to main menu\n
    """
    response = ""
    while not response:
        chars = "0123456789"
        chars += "abcdefghijklmnopqrstuvwxyz"
        chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        chars += "?%^*+~-=[]{}:,.#_"  # Oracle approved symbols
        legit = False
        while not legit:
            seed(int(crypto_100()))
            password = ""
            for _ in range(length):
                password += str(chars[randint(0, len(chars) - 1)])
            legit = audit(password, display=False)
        print("\033c\n   ", it("green", password), "\n")
        response = input(option_suggest.__doc__)
        print("")
        if response.isdigit():
            response = int(response)
            if response < 10:
                input("minimum suggested length is 10, press Enter to continue")
                length = 10
            if response >500:
                input("maximum supported length is 500, press Enter to continue")
                length = 500
            else:
                length = response
            response = ""
        elif response:
            wallet_main(passwords["master"]["master"], passwords)


def input_site_user():
    """
    routine to input site and user name
    """
    site = input("\nEnter site name:  ")
    if site == "master":
        user = "master"
    else:
        user = input("\nEnter user name:  ")
    print("")
    return site, user


def audit(password, display=True):
    """
    \nyour password is weak, you should change it!
    \naim for length greater or equal to 10
    \nand 2 each unique uppers, lowers, digits, symbols\n
    """
    uppers = []
    lowers = []
    digits = []
    others = []
    for item in [c for c in password]:
        if item.isupper():
            uppers.append(item)
        elif item.islower():
            lowers.append(item)
        elif item.isdigit():
            digits.append(item)
        else:
            others.append(item)
    length = len(password)
    uppers = len(list(set(uppers)))
    lowers = len(list(set(lowers)))
    digits = len(list(set(digits)))
    others = len(list(set(others)))
    review = {
        "length": length,
        "unique uppers": uppers,
        "unique lowers": lowers,
        "unique digits": digits,
        "unique symbols": others,
    }
    legit = True
    if not ((length >= 10) and (min(uppers, lowers, digits, others) >= 2)):
        legit = False
        if display:
            print(it("purple", audit.__doc__), it("green", "audit:"), review)
    return legit


if __name__ == "__main__":

    wallet_main()

UPDATE 1:

I have implemented all of the suggestions by Carcigenicate, visit my repo here for v0.00000004. Most noteworthy is complete refactoring of the wallet definitions. wallet_choices has been removed; I realized the double recursion was just to print the banner in both wallet_main and wallet_initialize, so I have created a banner definition. I also managed to eliminate the try/except and assert issues when inputting the choice. I redirected if __name__ = "__main__" to wallet_initialize. Additionally I added functionality to the password suggestion utility to copy to clipboard. Here are the new wallet methods:

def banner():
    """
    prepare a banner for use by wallet_main() and wallet_initialize()
    """
    return f"""
    ******************************************
    *** Welcome to CypherVault v{VERSION:.8f} ***
    ******************************************
    """

def wallet_main(passwords):
    """
    1: ENTER A NEW PASSWORD OR EDIT A PASSWORD
    2: DELETE A PASSWORD
    3: SUGGEST A PASSWORD
    4: PRINT SITE/USER LIST
    5: PRINT SITE/USER/PASSWORD LIST
    6: EXIT
    """
    crypto_indite(passwords)
    while True:
        print("\033c", it("green", banner()), it("green", wallet_main.__doc__))
        choice = input("input choice or press Enter to GET A PASSWORD: ") or "0"
        if choice.isdigit():
            choice = int(choice)
            if 0 <= choice <= 6:
                break
        print(f"\033c\n\n\n\tinvalid choice <{it('green', choice)}> try again")
        time.sleep(2)
    menu = {
        0: option_get,
        1: option_post,
        2: option_delete,
        3: option_suggest,
        4: option_print,
        5: option_print_full,
        6: crypto_indite,
    }
    menu[choice](passwords)
    if choice == 6:
        sys.exit()


def wallet_initialize(master):
    """
    initialize password dictionary and prompt for master password
    """
    # read password dictionary if none exists create a new encrypted cyphervault
    # all passwords are in format >>> passwords[site][user]
    print("\033c", it("green", banner()))
    try:
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        assert len(cyphervault) > 0
    except Exception:
        print("\nCypherVault not found, intitializing new...")
        doc_write("cyphervault.txt", (crypto_100() + "$n"))
        passwords = {"master": {"master": "password"}}
        crypto_indite(passwords)
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        print(it("purple", "\nyour default password is has been set to:"))
        print(it("green", "\npassword\n"))
    if master is None:
        master = getpass("Enter your master password:  ")
    # attempt to decrypt the cyphervault with the supplied password
    decrypted = False
    try:
        passwords = json_loads(crypto_decrypt(cyphervault, master))
        decrypted = True
    except Exception:
        trace()
        print(it("green", "\ninvalid master password, press Enter to try again..."))
        input("\npress Enter to return to main menu")
        wallet_initialize(master=None)  # recursion
    if decrypted:
        print(it("green", "\n    login successful!"))
        # warn if password is default
        if master == "password":
            print(it("purple", "\nyou should change default password immediately!"))
            print("\nyour master password is: ", it("green", master))
        # perform some tests on the password
        audit(master)
        wallet_main(passwords)

if __name__ == "__main__":
    wallet_initialize(master=None)

The only non-standard python library is pycryptodome found here. Do NOT use the deprecated pycrypto package.

# STANDARD PYTHON MODULES
import os
import sys
import time
import struct
import traceback
from hashlib import sha512
from hashlib import blake2b as blake
from hashlib import sha3_512 as sha3
from hashlib import pbkdf2_hmac as pbkdf
from hashlib import shake_256 as shake256
from base64 import b64encode, b64decode
from json import loads as json_loads
from json import dumps as json_dumps
from subprocess import Popen, PIPE
from random import seed, randint
from binascii import hexlify
from getpass import getpass
from pprint import pprint

# THIRD PARTY MODULES
from Crypto import Random
from Crypto.Cipher import AES


# USER DEFINED SECURITY CONSTANTS
# WARNING: when you change these parameters your CypherVault will need to be deleted
MEGABYTES = 400
ITERATIONS = 1000000

# CURRENT RELEASE ID
VERSION = 0.00000003


def it(style, text):
    """
    colored text in terminal
    """
    emphasis = {
        "red": 91,
        "green": 92,
        "yellow": 93,
        "blue": 94,
        "purple": 95,
        "cyan": 96,
    }
    return ("\033[%sm" % emphasis[style]) + str(text) + "\033[0m"


def trace():
    """
    Stack trace report upon exception
    """
    return "\n\n" + str(time.ctime()) + "\n\n" + str(traceback.format_exc()) + "\n\n"


def doc_write(document, text):
    """
    write a dictionary to file
    """
    with open(document, "w+") as handle:
        handle.write(text)
        handle.close()


def doc_read(document):
    """
    read dictionary from file
    """
    with open(document, "r") as handle:
        text = handle.read()
        handle.close()
        return text


def clip_get():
    """
    read from clipboard
    """
    clip = Popen(["xclip", "-selection", "clipboard", "-o"], stdout=PIPE)
    clip.wait()
    return clip.stdout.read().decode()


def clip_set(data):
    """
    write to clipboard
    """
    clip = Popen(["xclip", "-selection", "clipboard"], stdin=PIPE)
    clip.stdin.write(data.encode())
    clip.stdin.close()
    clip.wait()


def crypto_pad(msg):
    """
    pad if length is not a multiple of 128 else unpad as required
    """
    return (
        (msg + (128 - len(msg) % 128) * chr(128 - len(msg) % 128))
        if len(msg) % 128
        else msg[0 : -msg[-1]]
    )


def crypto_100():
    """
    cryptographically secure 100 digit string formatted integer generator
    """
    str_random = ""
    while len(str_random) != 100:
        set_random = struct.unpack("QQQQQQ", os.urandom(48))
        str_random = ""
        for integer in set_random:
            str_random += str(integer)
        str_random = str(int(str_random[-100:]))
    return str_random


def crypto_wacky_digest(msg_digest, salt):
    """
    never roll your own... except as a backup plan and fun learning experience!
    random amount of multiple hashing types to impose novelty restraint
    """
    shaken_salt = shake256(salt.encode()).digest(16)
    # randomized iteration count
    for _ in range(int(salt[-4:])):
        # for each up to 10 iterations; 1 in 10 no skip
        for _ in range(int(salt[-1])):
            # salted 512 chacha stream cipher with permuted input block copy
            msg_digest = hexlify(blake(msg_digest, salt=shaken_salt).digest())
        for _ in range(int(salt[-2])):
            # keccak 512 sponge construction
            msg_digest = sha3(msg_digest).digest()
        for _ in range(int(salt[-3])):
            # standard sha512
            msg_digest = sha512(msg_digest).digest()

    return msg_digest


def crypto_digest(password):
    """
    iterative rounds of hashing to impose time restraint
    expanded password length to impose memory restraint
    """
    # any scheme which uses more than a few hundred MB of RAM
    # is almost certainly inefficient for GPU or FPGA implementations
    password *= max(1, int(MEGABYTES * 10 ** 6 / len(password)))
    # shake and digest the salt to 16 bytes
    salt = doc_read("cyphervault.txt").split("$", 1)[0]
    shaken_salt = shake256(salt.encode()).digest(16)
    # many iterations of salted 512 password based key derivation function (PBKDF)
    msg_digest = hexlify(pbkdf("sha512", password.encode(), shaken_salt, ITERATIONS))
    # multiple types of hashing to impose novelty restraint
    msg_digest = crypto_wacky_digest(msg_digest, salt)
    # final format to sha256 for 32 byte output
    return shake256(msg_digest).digest(32)


def crypto_cypher(vector, password):
    """
    AES encryption method in CBC mode
    """
    return AES.new(crypto_digest(password), AES.MODE_CBC, vector, segment_size=256)


def crypto_encrypt(message, password):
    """
    encryption routine
    """
    vector = Random.new().read(AES.block_size)
    cypher = crypto_cypher(vector, password)
    recursion = cypher.encrypt(crypto_pad(message))
    return b64encode(vector + recursion)


def crypto_decrypt(cyphertext, password):
    """
    decryption routine
    """
    vector = b64decode(cyphertext)
    cypher = crypto_cypher(vector[:16], password)
    recursion = cypher.decrypt(vector[16:])
    return crypto_pad(recursion).decode()


def crypto_indite(passwords):
    """
    \nencrypting the CypherVault...
    """
    print(it("purple", crypto_indite.__doc__))
    cyphersalt = crypto_100()
    cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
    doc_write(
        "cyphervault.txt", cyphersalt + "$" + cyphervault,
    )
    cyphervault = crypto_encrypt(
        json_dumps(passwords), passwords["master"]["master"]
    ).decode()
    doc_write(
        "cyphervault.txt", cyphersalt + "$" + cyphervault,
    )


def wallet_main(master=None, passwords=None):
    """
    ******************************************
    *** Welcome to CypherVault v"""
    msg = "***\n    ******************************************\n"
    print("\033c", it("green", wallet_main.__doc__ + ("%.8f " % VERSION) + msg))
    if passwords is None:
        passwords = wallet_initialize(master)
    crypto_indite(passwords)
    choice = wallet_choices(passwords["master"]["master"], passwords)
    if choice == 0:
        option_get(passwords)
    elif choice == 1:
        option_post(passwords)
    elif choice == 2:
        option_delete(passwords)
    elif choice == 3:
        option_suggest(passwords)
    elif choice == 4:
        option_print(passwords)
    elif choice == 5:
        option_print_full(passwords)
    elif choice == 6:
        crypto_indite(passwords)
        sys.exit()


def wallet_initialize(master):
    """
    initialize password dictionary and prompt for master password
    """
    # read password dictionary if none exists create a new encrypted cyphervault
    # all passwords are in format >>> passwords[site][user]
    try:
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        assert len(cyphervault) > 0
    except Exception:
        print("\nCypherVault not found, intitializing new...")
        doc_write("cyphervault.txt", (crypto_100() + "$n"))
        passwords = {"master": {"master": "password"}}
        crypto_indite(passwords)
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        print(it("purple", "\nyour default password is has been set to:"))
        print(it("green", "\npassword\n"))
    if master is None:
        master = getpass("Enter your master password:  ")
    # attempt to decrypt the cyphervault with the supplied password
    decrypted = False
    try:
        passwords = json_loads(crypto_decrypt(cyphervault, master))
        decrypted = True
    except Exception:
        trace()
        print(it("green", "\ninvalid master password, press Enter to try again..."))
        input("\npress Enter to return to main menu")
        wallet_main()  # recursion
    if decrypted:
        # after every successful login create a new salt
        crypto_indite(passwords)
        print(it("green", "\n    login successful!"))
        # warn if password is default
        if master == "password":
            print(it("purple", "\nyou should change default password immediately!"))
            print("\nyour master password is: ", it("green", master))
        # perform some tests on the password
        audit(master)

        return passwords


def wallet_choices(master, passwords):
    """
    1: ENTER A NEW PASSWORD OR EDIT A PASSWORD
    2: DELETE A PASSWORD
    3: SUGGEST A PASSWORD
    4: PRINT SITE/USER LIST
    5: PRINT SITE/USER/PASSWORD LIST
    6: EXIT
    """
    print(it("green", wallet_choices.__doc__))
    choice = input("input choice or press Enter to GET A PASSWORD: ")
    if not choice:
        choice = 0
    try:
        choice = int(choice)
        assert 0 <= choice <= 6
    except Exception:
        print("\033cinvalid choice (", choice, ") try again")
        time.sleep(2)
        wallet_main(master, passwords)
    return choice


def option_get(passwords):
    """
    the password has been copied to the clipboard
    \nyou only have 10 seconds to paste it via ctrl+V
    """
    site, user = input_site_user()
    found = False
    if site in passwords.keys():
        if user in passwords[site].keys():
            found = True
            clip_set(passwords[site][user])
            print(it("purple", option_get.__doc__))
            time.sleep(10)
            clip_set("")
            print("clipboard has been cleared")
            time.sleep(2)
    if not found:
        print("\nsite/user not found in wallet")
        response = input("\nwould you like to add this site/user? (y/n):  ")
        if response in ["", "y", "Y"]:
            option_post(passwords, site, user)
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_post(passwords, site=None, user=None):
    """
    post a new or updated password to the cyphervault
    """

    def update(passwords, site, user):

        print(it("purple", "\nsite/user:"), site, user, "\n")
        # double Enter the new password
        new_pass = getpass("\ninput password: ")
        if new_pass == getpass("\ninput new password again: "):
            audit(new_pass)
            if site not in passwords.keys():
                passwords[site] = {}
            passwords[site][user] = new_pass
            crypto_indite(passwords)
            print(it("green", "\nCypherVault has been updated"))
        else:  # recursion
            print(it("purple", "\npasswords do not match, try again..."))
            time.sleep(2)
            update(passwords, site, user)

    if site is None:
        site, user = input_site_user()
    create_new = True
    # update a password if it already exists
    if site in passwords.keys():
        if user in passwords[site].keys():
            print("\nsite:", site, "\nuser:", user)
            print(it("purple", "\nWARN: site/user already exists"))
            response = input("\nwould you like to overwrite this site/user? (y/n):")
            if response not in ["", "y", "Y"]:
                create_new = False
    if create_new:
        update(passwords, site, user)
    # return to main menu
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_delete(passwords):
    """
    remove a user from the cyphervault
    """
    print("\nEnter the site and user you would like to delete")
    site, user = input_site_user()
    if site != "master":
        found = False
        # check if the site/user exists in the passwords
        if site in passwords.keys():
            if user in passwords[site].keys():
                found = True
        if found:
            # remove the user
            del passwords[site][user]
            if passwords[site] == {}:
                # if there are no other users at that site, remove the site as well
                del passwords[site]
                crypto_indite(passwords)
                print(it("purple", "\nsite/user has been deleted"))
                time.sleep(2)
        else:
            # return to the main menu
            print(it("purple", "\nsite/user was not found"))

    else:
        print(it("purple", "you cannot delete the master password!"))
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_print(passwords):
    """
    print all site/user combinations in the cyphervault
    """
    print("")
    for site, logins in passwords.items():
        for user, _ in logins.items():
            print(it("green", site + " : " + user))
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_print_full(passwords):
    """
    \n\n
    WARNING: YOU ARE ABOUT TO EXPOSE YOUR UNENCRYPTED CYPHERVAULT
    Make sure you are in a private location!\n
    WARNING: DO NOT PRINT THIS TO PAPER
    CypherVault contents will be exposed on your printer hard drive\n
    WARNING: DO NOT SAVE THIS TO FILE
    CypherVault contents will be exposed on your local hard drive\n
    USE THIS FUNCTION ONLY TO INSPECT OR COPY - BY HAND - TO PAPER\n\n
    """
    print("\033c", it("purple", option_print_full.__doc__))
    msg = "\nare you sure you want to print your unencrypted CyperVault (y/n):  "
    response = input(it("green", msg))
    if response in ["y", "Y"]:
        msg = "\npress Enter to expose the CypherVault\npress Enter again to exit\n"
        print(it("green", msg))
        input()
        print("\033c\n\n\n")
        pprint(passwords)
    input(it("green", "\npress Enter to return to main menu"))
    wallet_main(passwords["master"]["master"], passwords)


def option_suggest(passwords, length=10):
    """
    press Enter to suggest another secure random password
    or any number 10 to 500, then Enter to change the length
    or any other key, then Enter to return to main menu\n
    """
    response = ""
    while not response:
        chars = "0123456789"
        chars += "abcdefghijklmnopqrstuvwxyz"
        chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        chars += "?%^*+~-=[]{}:,.#_"  # Oracle approved symbols
        legit = False
        while not legit:
            seed(int(crypto_100()))
            password = ""
            for _ in range(length):
                password += str(chars[randint(0, len(chars) - 1)])
            legit = audit(password, display=False)
        print("\033c\n   ", it("green", password), "\n")
        response = input(option_suggest.__doc__)
        print("")
        if response.isdigit():
            response = int(response)
            if response < 10:
                input("minimum suggested length is 10, press Enter to continue")
                length = 10
            if response >500:
                input("maximum supported length is 500, press Enter to continue")
                length = 500
            else:
                length = response
            response = ""
        elif response:
            wallet_main(passwords["master"]["master"], passwords)


def input_site_user():
    """
    routine to input site and user name
    """
    site = input("\nEnter site name:  ")
    if site == "master":
        user = "master"
    else:
        user = input("\nEnter user name:  ")
    print("")
    return site, user


def audit(password, display=True):
    """
    \nyour password is weak, you should change it!
    \naim for length greater or equal to 10
    \nand 2 each unique uppers, lowers, digits, symbols\n
    """
    uppers = []
    lowers = []
    digits = []
    others = []
    for item in [c for c in password]:
        if item.isupper():
            uppers.append(item)
        elif item.islower():
            lowers.append(item)
        elif item.isdigit():
            digits.append(item)
        else:
            others.append(item)
    length = len(password)
    uppers = len(list(set(uppers)))
    lowers = len(list(set(lowers)))
    digits = len(list(set(digits)))
    others = len(list(set(others)))
    review = {
        "length": length,
        "unique uppers": uppers,
        "unique lowers": lowers,
        "unique digits": digits,
        "unique symbols": others,
    }
    legit = True
    if not ((length >= 10) and (min(uppers, lowers, digits, others) >= 2)):
        legit = False
        if display:
            print(it("purple", audit.__doc__), it("green", "audit:"), review)
    return legit


if __name__ == "__main__":

    wallet_main()

UPDATE 1:

I have implemented all of the suggestions by Carcigenicate, visit my repo here for v0.00000004. Most noteworthy is complete refactoring of the wallet definitions. wallet_choices has been removed; I realized the double recursion was just to print the banner in both wallet_main and wallet_initialize, so I have created a banner definition. I also managed to eliminate the try/except and assert issues when inputting the choice. I redirected if __name__ = "__main__" to wallet_initialize. Additionally I added functionality to the password suggestion utility to copy to clipboard. Here are the new wallet methods:

def banner():
    """
    prepare a banner for use by wallet_main() and wallet_initialize()
    """
    return f"""
    ******************************************
    *** Welcome to CypherVault v{VERSION:.8f} ***
    ******************************************
    """

def wallet_main(passwords):
    """
    1: ENTER A NEW PASSWORD OR EDIT A PASSWORD
    2: DELETE A PASSWORD
    3: SUGGEST A PASSWORD
    4: PRINT SITE/USER LIST
    5: PRINT SITE/USER/PASSWORD LIST
    6: EXIT
    """
    crypto_indite(passwords)
    while True:
        print("\033c", it("green", banner()), it("green", wallet_main.__doc__))
        choice = input("input choice or press Enter to GET A PASSWORD: ") or "0"
        if choice.isdigit():
            choice = int(choice)
            if 0 <= choice <= 6:
                break
        print(f"\033c\n\n\n\tinvalid choice <{it('green', choice)}> try again")
        time.sleep(2)
    menu = {
        0: option_get,
        1: option_post,
        2: option_delete,
        3: option_suggest,
        4: option_print,
        5: option_print_full,
        6: crypto_indite,
    }
    menu[choice](passwords)
    if choice == 6:
        sys.exit()


def wallet_initialize(master):
    """
    initialize password dictionary and prompt for master password
    """
    # read password dictionary if none exists create a new encrypted cyphervault
    # all passwords are in format >>> passwords[site][user]
    print("\033c", it("green", banner()))
    try:
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        assert len(cyphervault) > 0
    except Exception:
        print("\nCypherVault not found, intitializing new...")
        doc_write("cyphervault.txt", (crypto_100() + "$n"))
        passwords = {"master": {"master": "password"}}
        crypto_indite(passwords)
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        print(it("purple", "\nyour default password is has been set to:"))
        print(it("green", "\npassword\n"))
    if master is None:
        master = getpass("Enter your master password:  ")
    # attempt to decrypt the cyphervault with the supplied password
    decrypted = False
    try:
        passwords = json_loads(crypto_decrypt(cyphervault, master))
        decrypted = True
    except Exception:
        trace()
        print(it("green", "\ninvalid master password, press Enter to try again..."))
        input("\npress Enter to return to main menu")
        wallet_initialize(master=None)  # recursion
    if decrypted:
        print(it("green", "\n    login successful!"))
        # warn if password is default
        if master == "password":
            print(it("purple", "\nyou should change default password immediately!"))
            print("\nyour master password is: ", it("green", master))
        # perform some tests on the password
        audit(master)
        wallet_main(passwords)

if __name__ == "__main__":
    wallet_initialize(master=None)

The only non-standard python library is here.

# STANDARD PYTHON MODULES
import os
import sys
import time
import struct
import traceback
from hashlib import sha512
from hashlib import blake2b as blake
from hashlib import sha3_512 as sha3
from hashlib import pbkdf2_hmac as pbkdf
from hashlib import shake_256 as shake256
from base64 import b64encode, b64decode
from json import loads as json_loads
from json import dumps as json_dumps
from subprocess import Popen, PIPE
from random import seed, randint
from binascii import hexlify
from getpass import getpass
from pprint import pprint

# THIRD PARTY MODULES
from Crypto import Random
from Crypto.Cipher import AES


# USER DEFINED SECURITY CONSTANTS
# WARNING: when you change these parameters your CypherVault will need to be deleted
MEGABYTES = 400
ITERATIONS = 1000000

# CURRENT RELEASE ID
VERSION = 0.00000003


def it(style, text):
    """
    colored text in terminal
    """
    emphasis = {
        "red": 91,
        "green": 92,
        "yellow": 93,
        "blue": 94,
        "purple": 95,
        "cyan": 96,
    }
    return ("\033[%sm" % emphasis[style]) + str(text) + "\033[0m"


def trace():
    """
    Stack trace report upon exception
    """
    return "\n\n" + str(time.ctime()) + "\n\n" + str(traceback.format_exc()) + "\n\n"


def doc_write(document, text):
    """
    write a dictionary to file
    """
    with open(document, "w+") as handle:
        handle.write(text)
        handle.close()


def doc_read(document):
    """
    read dictionary from file
    """
    with open(document, "r") as handle:
        text = handle.read()
        handle.close()
        return text


def clip_get():
    """
    read from clipboard
    """
    clip = Popen(["xclip", "-selection", "clipboard", "-o"], stdout=PIPE)
    clip.wait()
    return clip.stdout.read().decode()


def clip_set(data):
    """
    write to clipboard
    """
    clip = Popen(["xclip", "-selection", "clipboard"], stdin=PIPE)
    clip.stdin.write(data.encode())
    clip.stdin.close()
    clip.wait()


def crypto_pad(msg):
    """
    pad if length is not a multiple of 128 else unpad as required
    """
    return (
        (msg + (128 - len(msg) % 128) * chr(128 - len(msg) % 128))
        if len(msg) % 128
        else msg[0 : -msg[-1]]
    )


def crypto_100():
    """
    cryptographically secure 100 digit string formatted integer generator
    """
    str_random = ""
    while len(str_random) != 100:
        set_random = struct.unpack("QQQQQQ", os.urandom(48))
        str_random = ""
        for integer in set_random:
            str_random += str(integer)
        str_random = str(int(str_random[-100:]))
    return str_random


def crypto_wacky_digest(msg_digest, salt):
    """
    never roll your own... except as a backup plan and fun learning experience!
    random amount of multiple hashing types to impose novelty restraint
    """
    shaken_salt = shake256(salt.encode()).digest(16)
    # randomized iteration count
    for _ in range(int(salt[-4:])):
        # for each up to 10 iterations; 1 in 10 no skip
        for _ in range(int(salt[-1])):
            # salted 512 chacha stream cipher with permuted input block copy
            msg_digest = hexlify(blake(msg_digest, salt=shaken_salt).digest())
        for _ in range(int(salt[-2])):
            # keccak 512 sponge construction
            msg_digest = sha3(msg_digest).digest()
        for _ in range(int(salt[-3])):
            # standard sha512
            msg_digest = sha512(msg_digest).digest()

    return msg_digest


def crypto_digest(password):
    """
    iterative rounds of hashing to impose time restraint
    expanded password length to impose memory restraint
    """
    # any scheme which uses more than a few hundred MB of RAM
    # is almost certainly inefficient for GPU or FPGA implementations
    password *= max(1, int(MEGABYTES * 10 ** 6 / len(password)))
    # shake and digest the salt to 16 bytes
    salt = doc_read("cyphervault.txt").split("$", 1)[0]
    shaken_salt = shake256(salt.encode()).digest(16)
    # many iterations of salted 512 password based key derivation function (PBKDF)
    msg_digest = hexlify(pbkdf("sha512", password.encode(), shaken_salt, ITERATIONS))
    # multiple types of hashing to impose novelty restraint
    msg_digest = crypto_wacky_digest(msg_digest, salt)
    # final format to sha256 for 32 byte output
    return shake256(msg_digest).digest(32)


def crypto_cypher(vector, password):
    """
    AES encryption method in CBC mode
    """
    return AES.new(crypto_digest(password), AES.MODE_CBC, vector, segment_size=256)


def crypto_encrypt(message, password):
    """
    encryption routine
    """
    vector = Random.new().read(AES.block_size)
    cypher = crypto_cypher(vector, password)
    recursion = cypher.encrypt(crypto_pad(message))
    return b64encode(vector + recursion)


def crypto_decrypt(cyphertext, password):
    """
    decryption routine
    """
    vector = b64decode(cyphertext)
    cypher = crypto_cypher(vector[:16], password)
    recursion = cypher.decrypt(vector[16:])
    return crypto_pad(recursion).decode()


def crypto_indite(passwords):
    """
    \nencrypting the CypherVault...
    """
    print(it("purple", crypto_indite.__doc__))
    cyphersalt = crypto_100()
    cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
    doc_write(
        "cyphervault.txt", cyphersalt + "$" + cyphervault,
    )
    cyphervault = crypto_encrypt(
        json_dumps(passwords), passwords["master"]["master"]
    ).decode()
    doc_write(
        "cyphervault.txt", cyphersalt + "$" + cyphervault,
    )


def wallet_main(master=None, passwords=None):
    """
    ******************************************
    *** Welcome to CypherVault v"""
    msg = "***\n    ******************************************\n"
    print("\033c", it("green", wallet_main.__doc__ + ("%.8f " % VERSION) + msg))
    if passwords is None:
        passwords = wallet_initialize(master)
    crypto_indite(passwords)
    choice = wallet_choices(passwords["master"]["master"], passwords)
    if choice == 0:
        option_get(passwords)
    elif choice == 1:
        option_post(passwords)
    elif choice == 2:
        option_delete(passwords)
    elif choice == 3:
        option_suggest(passwords)
    elif choice == 4:
        option_print(passwords)
    elif choice == 5:
        option_print_full(passwords)
    elif choice == 6:
        crypto_indite(passwords)
        sys.exit()


def wallet_initialize(master):
    """
    initialize password dictionary and prompt for master password
    """
    # read password dictionary if none exists create a new encrypted cyphervault
    # all passwords are in format >>> passwords[site][user]
    try:
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        assert len(cyphervault) > 0
    except Exception:
        print("\nCypherVault not found, intitializing new...")
        doc_write("cyphervault.txt", (crypto_100() + "$n"))
        passwords = {"master": {"master": "password"}}
        crypto_indite(passwords)
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        print(it("purple", "\nyour default password is has been set to:"))
        print(it("green", "\npassword\n"))
    if master is None:
        master = getpass("Enter your master password:  ")
    # attempt to decrypt the cyphervault with the supplied password
    decrypted = False
    try:
        passwords = json_loads(crypto_decrypt(cyphervault, master))
        decrypted = True
    except Exception:
        trace()
        print(it("green", "\ninvalid master password, press Enter to try again..."))
        input("\npress Enter to return to main menu")
        wallet_main()  # recursion
    if decrypted:
        # after every successful login create a new salt
        crypto_indite(passwords)
        print(it("green", "\n    login successful!"))
        # warn if password is default
        if master == "password":
            print(it("purple", "\nyou should change default password immediately!"))
            print("\nyour master password is: ", it("green", master))
        # perform some tests on the password
        audit(master)

        return passwords


def wallet_choices(master, passwords):
    """
    1: ENTER A NEW PASSWORD OR EDIT A PASSWORD
    2: DELETE A PASSWORD
    3: SUGGEST A PASSWORD
    4: PRINT SITE/USER LIST
    5: PRINT SITE/USER/PASSWORD LIST
    6: EXIT
    """
    print(it("green", wallet_choices.__doc__))
    choice = input("input choice or press Enter to GET A PASSWORD: ")
    if not choice:
        choice = 0
    try:
        choice = int(choice)
        assert 0 <= choice <= 6
    except Exception:
        print("\033cinvalid choice (", choice, ") try again")
        time.sleep(2)
        wallet_main(master, passwords)
    return choice


def option_get(passwords):
    """
    the password has been copied to the clipboard
    \nyou only have 10 seconds to paste it via ctrl+V
    """
    site, user = input_site_user()
    found = False
    if site in passwords.keys():
        if user in passwords[site].keys():
            found = True
            clip_set(passwords[site][user])
            print(it("purple", option_get.__doc__))
            time.sleep(10)
            clip_set("")
            print("clipboard has been cleared")
            time.sleep(2)
    if not found:
        print("\nsite/user not found in wallet")
        response = input("\nwould you like to add this site/user? (y/n):  ")
        if response in ["", "y", "Y"]:
            option_post(passwords, site, user)
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_post(passwords, site=None, user=None):
    """
    post a new or updated password to the cyphervault
    """

    def update(passwords, site, user):

        print(it("purple", "\nsite/user:"), site, user, "\n")
        # double Enter the new password
        new_pass = getpass("\ninput password: ")
        if new_pass == getpass("\ninput new password again: "):
            audit(new_pass)
            if site not in passwords.keys():
                passwords[site] = {}
            passwords[site][user] = new_pass
            crypto_indite(passwords)
            print(it("green", "\nCypherVault has been updated"))
        else:  # recursion
            print(it("purple", "\npasswords do not match, try again..."))
            time.sleep(2)
            update(passwords, site, user)

    if site is None:
        site, user = input_site_user()
    create_new = True
    # update a password if it already exists
    if site in passwords.keys():
        if user in passwords[site].keys():
            print("\nsite:", site, "\nuser:", user)
            print(it("purple", "\nWARN: site/user already exists"))
            response = input("\nwould you like to overwrite this site/user? (y/n):")
            if response not in ["", "y", "Y"]:
                create_new = False
    if create_new:
        update(passwords, site, user)
    # return to main menu
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_delete(passwords):
    """
    remove a user from the cyphervault
    """
    print("\nEnter the site and user you would like to delete")
    site, user = input_site_user()
    if site != "master":
        found = False
        # check if the site/user exists in the passwords
        if site in passwords.keys():
            if user in passwords[site].keys():
                found = True
        if found:
            # remove the user
            del passwords[site][user]
            if passwords[site] == {}:
                # if there are no other users at that site, remove the site as well
                del passwords[site]
                crypto_indite(passwords)
                print(it("purple", "\nsite/user has been deleted"))
                time.sleep(2)
        else:
            # return to the main menu
            print(it("purple", "\nsite/user was not found"))

    else:
        print(it("purple", "you cannot delete the master password!"))
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_print(passwords):
    """
    print all site/user combinations in the cyphervault
    """
    print("")
    for site, logins in passwords.items():
        for user, _ in logins.items():
            print(it("green", site + " : " + user))
    input("\npress Enter to return to main menu")
    wallet_main(passwords["master"]["master"], passwords)


def option_print_full(passwords):
    """
    \n\n
    WARNING: YOU ARE ABOUT TO EXPOSE YOUR UNENCRYPTED CYPHERVAULT
    Make sure you are in a private location!\n
    WARNING: DO NOT PRINT THIS TO PAPER
    CypherVault contents will be exposed on your printer hard drive\n
    WARNING: DO NOT SAVE THIS TO FILE
    CypherVault contents will be exposed on your local hard drive\n
    USE THIS FUNCTION ONLY TO INSPECT OR COPY - BY HAND - TO PAPER\n\n
    """
    print("\033c", it("purple", option_print_full.__doc__))
    msg = "\nare you sure you want to print your unencrypted CyperVault (y/n):  "
    response = input(it("green", msg))
    if response in ["y", "Y"]:
        msg = "\npress Enter to expose the CypherVault\npress Enter again to exit\n"
        print(it("green", msg))
        input()
        print("\033c\n\n\n")
        pprint(passwords)
    input(it("green", "\npress Enter to return to main menu"))
    wallet_main(passwords["master"]["master"], passwords)


def option_suggest(passwords, length=10):
    """
    press Enter to suggest another secure random password
    or any number 10 to 500, then Enter to change the length
    or any other key, then Enter to return to main menu\n
    """
    response = ""
    while not response:
        chars = "0123456789"
        chars += "abcdefghijklmnopqrstuvwxyz"
        chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        chars += "?%^*+~-=[]{}:,.#_"  # Oracle approved symbols
        legit = False
        while not legit:
            seed(int(crypto_100()))
            password = ""
            for _ in range(length):
                password += str(chars[randint(0, len(chars) - 1)])
            legit = audit(password, display=False)
        print("\033c\n   ", it("green", password), "\n")
        response = input(option_suggest.__doc__)
        print("")
        if response.isdigit():
            response = int(response)
            if response < 10:
                input("minimum suggested length is 10, press Enter to continue")
                length = 10
            if response >500:
                input("maximum supported length is 500, press Enter to continue")
                length = 500
            else:
                length = response
            response = ""
        elif response:
            wallet_main(passwords["master"]["master"], passwords)


def input_site_user():
    """
    routine to input site and user name
    """
    site = input("\nEnter site name:  ")
    if site == "master":
        user = "master"
    else:
        user = input("\nEnter user name:  ")
    print("")
    return site, user


def audit(password, display=True):
    """
    \nyour password is weak, you should change it!
    \naim for length greater or equal to 10
    \nand 2 each unique uppers, lowers, digits, symbols\n
    """
    uppers = []
    lowers = []
    digits = []
    others = []
    for item in [c for c in password]:
        if item.isupper():
            uppers.append(item)
        elif item.islower():
            lowers.append(item)
        elif item.isdigit():
            digits.append(item)
        else:
            others.append(item)
    length = len(password)
    uppers = len(list(set(uppers)))
    lowers = len(list(set(lowers)))
    digits = len(list(set(digits)))
    others = len(list(set(others)))
    review = {
        "length": length,
        "unique uppers": uppers,
        "unique lowers": lowers,
        "unique digits": digits,
        "unique symbols": others,
    }
    legit = True
    if not ((length >= 10) and (min(uppers, lowers, digits, others) >= 2)):
        legit = False
        if display:
            print(it("purple", audit.__doc__), it("green", "audit:"), review)
    return legit


if __name__ == "__main__":

    wallet_main()
Rollback to Revision 5
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127

UPDATE 1: v0.00000004 UPDATE 1:

def banner():
    """
    prepare a banner for use by wallet_main() and wallet_initialize()
    """
    return f"""
    ******************************************
    *** Welcome to CypherVault v{VERSION:.8f} ***
    ******************************************
    """

def wallet_main(passwords):
    """
    1: ENTER A NEW PASSWORD OR EDIT A PASSWORD
    2: DELETE A PASSWORD
    3: SUGGEST A PASSWORD
    4: PRINT SITE/USER LIST
    5: PRINT SITE/USER/PASSWORD LIST
    6: EXIT
    """
    crypto_indite(passwords)
    while True:
        print("\033c", it("green", banner()), it("green", wallet_main.__doc__))
        choice = input("input choice or press Enter to GET A PASSWORD: ") or "0"
        if choice.isdigit():
            choice = int(choice)
            if 0 <= choice <= 6:
                break
        print(f"\033c\n\n\n\tinvalid choice <{it('green', choice)}> try again")
        time.sleep(2)
    menu = {
        0: option_get,
        1: option_post,
        2: option_delete,
        3: option_suggest,
        4: option_print,
        5: option_print_full,
        6: crypto_indite,
    }
    menu[choice](passwords)
    if choice == 6:
        sys.exit()


def wallet_initialize(master):
    """
    initialize password dictionary and prompt for master password
    """
    # read password dictionary if none exists create a new encrypted cyphervault
    # all passwords are in format >>> passwords[site][user]
    print("\033c", it("green", banner()))
    try:
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        assert len(cyphervault) > 0
    except Exception:
        print("\nCypherVault not found, intitializing new...")
        doc_write("cyphervault.txt", (crypto_100() + "$n"))
        passwords = {"master": {"master": "password"}}
        crypto_indite(passwords)
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        print(it("purple", "\nyour default password is has been set to:"))
        print(it("green", "\npassword\n"))
    if master is None:
        master = getpass("Enter your master password:  ")
    # attempt to decrypt the cyphervault with the supplied password
    decrypted = False
    try:
        passwords = json_loads(crypto_decrypt(cyphervault, master))
        decrypted = True
    except Exception:
        trace()
        print(it("green", "\ninvalid master password, press Enter to try again..."))
        input("\npress Enter to return to main menu")
        wallet_initialize(master=None)  # recursion
    if decrypted:
        print(it("green", "\n    login successful!"))
        # warn if password is default
        if master == "password":
            print(it("purple", "\nyou should change default password immediately!"))
            print("\nyour master password is: ", it("green", master))
        # perform some tests on the password
        audit(master)
        wallet_main(passwords)

if __name__ == "__main__":
    wallet_initialize(master=None)

UPDATE 2: v0.00000005

Seeking review on 5 additional features added as stand alone definitions:

Visit my repo here for complete implementation.

read_only() (called once by wallet_initialize)

  • ensures user must "Open as root" to edit cyphervault.py. This prevents the injection of malicious script, which could expose passwords after authentication.

pycryptodome() (called once by wallet_initialize)

  • ensures that pycryptodome is installed and deprecated pycrypto is NOT

sites_without() (called every loop by wallet_main)

  • maintains a json formatted list of sites and user (without passwords) in human readable text file

option_import_json() (called by user choice in wallet_main)

  • allows for the user to cut and paste properly json formatted password list and append it to the cyphervault for bulk addition of passwords

cluster() (called by option_suggest prior to suggesting new password)

  • makes suggested passwords easier to remember while still maintaining randomness by clustering uppers, lowers, digits, symbols in blocks of 2-4

new definitions:

def read_only():
    """
    require sudo to edit CypherVault.py
    """
    PATH = str(os.path.dirname(os.path.abspath(__file__))) + "/"
    os.chmod(PATH+"cyphervault.py", S_IREAD)
    
def pycryptodome():
    """
    \nTEST FAILED!\n\n
    "pycrypto" is not maintained and has been replaced by "pycryptodome"\n
    packages installed on your system:\n
    """
    print("\033c\n\nensuring", it("green","pycryptodome"), "is installed...")   
    print("ensuring pycrypto is", it("purple","NOT"), "installed...")
    p = Popen(["pip", "show", "pycrypto"], stdout=PIPE)
    out_pycrypto, _ = p.communicate()
    p = Popen(["pip", "show", "pycryptodome"], stdout=PIPE)
    out_pycryptodome, _ = p.communicate()
    out_pycrypto = out_pycrypto.decode()
    out_pycryptodome = out_pycryptodome.decode()
    if (("Version" in out_pycrypto) or not ("Version" in out_pycryptodome)):
        print(it("purple", pycryptodome.__doc__), out_pycryptodome, out_pycrypto)
        raise ValueError(it("green", "pip3 install pycryptodome"))
    else:
        print(it("purple", "\n\nTEST PASSED!\n\n"))
        time.sleep(1)
    
def sites_without(passwords):
    """
    maintain a list of user accounts in JSON format in CypherVault_accounts.txt
    """
    accounts = {k:{k2:"" for k2,v2 in v.items()} for k,v in passwords.items()}
    doc_write(
        "CypherVault_accounts.txt",
        json_dumps(accounts, indent=0, sort_keys=True)
    )

def option_import_json(passwords):
    """
    use this utility to import a password dictionary
    via cut and paste from text editor
    it must be properly formatted JSON, example:\n
    {"site_name":{"user_name":"your_password"}}\n
    which is the same format as menu choice\n
    PRINT SITE/USER/PASSWORD LIST JSON\n
    for email addresses site and user must match\n
    DO NOT save the text document!
    use an "untitled" text document,
    so it does not autosave and remains in RAM!\n
    to skip and return to main menu, press Enter\n 
    """
    print("\033c\n\n\n", it("green", option_import_json.__doc__))
    json_doc = input("enter your JSON formatted password list:  ")
    if json_doc:
        try:
            dictionary = json_loads(json_doc)
            try:
                d_is = isinstance(dictionary, dict)
                for k, v in dictionary.items():
                    k_is = isinstance(k, str)
                    v_is = isinstance(v, dict)
                    for k2, v2 in v.items():
                        k2_is = isinstance(k, str)
                        v2_is = isinstance(k, str)
                valid = d_is and k_is and v_is and k2_is and v2_is
                if not valid:
                    raise ValueError
            except ValueError:
                trace()
                msg = """
                    improperly formatted passwords dictionary\n\n
                    press Enter to return to main menu
                    """
                input(msg)
                wallet_main(passwords)
            passwords.update(dictionary)
            crypto_indite(passwords)
            print(it("green", "\nCypherVault successfully updated!"))
            wallet_main(passwords)
        except ValueError:
            trace()
            input("invalid json\n\npress Enter to return to main menu")
    clip_set("")
    wallet_main(passwords)

def cluster(password):
    """
    format password into blocks of numbers, letters, and symbols 
    """

    def split(string):
        """
        split string into blocks
        """
        length = randint(3,4)
        return [string[i : i + length] for i in range(0, len(string), length)]

    digits = ""
    uppers = ""
    lowers = ""
    others = ""
    for char in password:
        if char.isdigit():
            digits += char
        elif char.isupper():
            uppers += char
        elif char.islower():
            lowers += char
        else:
            others += char

    cluster_list = (
        split(digits) + split(lowers) + split(uppers) + [i for i in others]
    )
    shuffle(cluster_list)

    return "".join(cluster_list)

UPDATE 1: v0.00000004

def banner():
    """
    prepare a banner for use by wallet_main() and wallet_initialize()
    """
    return f"""
    ******************************************
    *** Welcome to CypherVault v{VERSION:.8f} ***
    ******************************************
    """

def wallet_main(passwords):
    """
    1: ENTER A NEW PASSWORD OR EDIT A PASSWORD
    2: DELETE A PASSWORD
    3: SUGGEST A PASSWORD
    4: PRINT SITE/USER LIST
    5: PRINT SITE/USER/PASSWORD LIST
    6: EXIT
    """
    crypto_indite(passwords)
    while True:
        print("\033c", it("green", banner()), it("green", wallet_main.__doc__))
        choice = input("input choice or press Enter to GET A PASSWORD: ") or "0"
        if choice.isdigit():
            choice = int(choice)
            if 0 <= choice <= 6:
                break
        print(f"\033c\n\n\n\tinvalid choice <{it('green', choice)}> try again")
        time.sleep(2)
    menu = {
        0: option_get,
        1: option_post,
        2: option_delete,
        3: option_suggest,
        4: option_print,
        5: option_print_full,
        6: crypto_indite,
    }
    menu[choice](passwords)
    if choice == 6:
        sys.exit()


def wallet_initialize(master):
    """
    initialize password dictionary and prompt for master password
    """
    # read password dictionary if none exists create a new encrypted cyphervault
    # all passwords are in format >>> passwords[site][user]
    print("\033c", it("green", banner()))
    try:
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        assert len(cyphervault) > 0
    except Exception:
        print("\nCypherVault not found, intitializing new...")
        doc_write("cyphervault.txt", (crypto_100() + "$n"))
        passwords = {"master": {"master": "password"}}
        crypto_indite(passwords)
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        print(it("purple", "\nyour default password is has been set to:"))
        print(it("green", "\npassword\n"))
    if master is None:
        master = getpass("Enter your master password:  ")
    # attempt to decrypt the cyphervault with the supplied password
    decrypted = False
    try:
        passwords = json_loads(crypto_decrypt(cyphervault, master))
        decrypted = True
    except Exception:
        trace()
        print(it("green", "\ninvalid master password, press Enter to try again..."))
        input("\npress Enter to return to main menu")
        wallet_initialize(master=None)  # recursion
    if decrypted:
        print(it("green", "\n    login successful!"))
        # warn if password is default
        if master == "password":
            print(it("purple", "\nyou should change default password immediately!"))
            print("\nyour master password is: ", it("green", master))
        # perform some tests on the password
        audit(master)
        wallet_main(passwords)

if __name__ == "__main__":
    wallet_initialize(master=None)

UPDATE 2: v0.00000005

Seeking review on 5 additional features added as stand alone definitions:

Visit my repo here for complete implementation.

read_only() (called once by wallet_initialize)

  • ensures user must "Open as root" to edit cyphervault.py. This prevents the injection of malicious script, which could expose passwords after authentication.

pycryptodome() (called once by wallet_initialize)

  • ensures that pycryptodome is installed and deprecated pycrypto is NOT

sites_without() (called every loop by wallet_main)

  • maintains a json formatted list of sites and user (without passwords) in human readable text file

option_import_json() (called by user choice in wallet_main)

  • allows for the user to cut and paste properly json formatted password list and append it to the cyphervault for bulk addition of passwords

cluster() (called by option_suggest prior to suggesting new password)

  • makes suggested passwords easier to remember while still maintaining randomness by clustering uppers, lowers, digits, symbols in blocks of 2-4

new definitions:

def read_only():
    """
    require sudo to edit CypherVault.py
    """
    PATH = str(os.path.dirname(os.path.abspath(__file__))) + "/"
    os.chmod(PATH+"cyphervault.py", S_IREAD)
    
def pycryptodome():
    """
    \nTEST FAILED!\n\n
    "pycrypto" is not maintained and has been replaced by "pycryptodome"\n
    packages installed on your system:\n
    """
    print("\033c\n\nensuring", it("green","pycryptodome"), "is installed...")   
    print("ensuring pycrypto is", it("purple","NOT"), "installed...")
    p = Popen(["pip", "show", "pycrypto"], stdout=PIPE)
    out_pycrypto, _ = p.communicate()
    p = Popen(["pip", "show", "pycryptodome"], stdout=PIPE)
    out_pycryptodome, _ = p.communicate()
    out_pycrypto = out_pycrypto.decode()
    out_pycryptodome = out_pycryptodome.decode()
    if (("Version" in out_pycrypto) or not ("Version" in out_pycryptodome)):
        print(it("purple", pycryptodome.__doc__), out_pycryptodome, out_pycrypto)
        raise ValueError(it("green", "pip3 install pycryptodome"))
    else:
        print(it("purple", "\n\nTEST PASSED!\n\n"))
        time.sleep(1)
    
def sites_without(passwords):
    """
    maintain a list of user accounts in JSON format in CypherVault_accounts.txt
    """
    accounts = {k:{k2:"" for k2,v2 in v.items()} for k,v in passwords.items()}
    doc_write(
        "CypherVault_accounts.txt",
        json_dumps(accounts, indent=0, sort_keys=True)
    )

def option_import_json(passwords):
    """
    use this utility to import a password dictionary
    via cut and paste from text editor
    it must be properly formatted JSON, example:\n
    {"site_name":{"user_name":"your_password"}}\n
    which is the same format as menu choice\n
    PRINT SITE/USER/PASSWORD LIST JSON\n
    for email addresses site and user must match\n
    DO NOT save the text document!
    use an "untitled" text document,
    so it does not autosave and remains in RAM!\n
    to skip and return to main menu, press Enter\n 
    """
    print("\033c\n\n\n", it("green", option_import_json.__doc__))
    json_doc = input("enter your JSON formatted password list:  ")
    if json_doc:
        try:
            dictionary = json_loads(json_doc)
            try:
                d_is = isinstance(dictionary, dict)
                for k, v in dictionary.items():
                    k_is = isinstance(k, str)
                    v_is = isinstance(v, dict)
                    for k2, v2 in v.items():
                        k2_is = isinstance(k, str)
                        v2_is = isinstance(k, str)
                valid = d_is and k_is and v_is and k2_is and v2_is
                if not valid:
                    raise ValueError
            except ValueError:
                trace()
                msg = """
                    improperly formatted passwords dictionary\n\n
                    press Enter to return to main menu
                    """
                input(msg)
                wallet_main(passwords)
            passwords.update(dictionary)
            crypto_indite(passwords)
            print(it("green", "\nCypherVault successfully updated!"))
            wallet_main(passwords)
        except ValueError:
            trace()
            input("invalid json\n\npress Enter to return to main menu")
    clip_set("")
    wallet_main(passwords)

def cluster(password):
    """
    format password into blocks of numbers, letters, and symbols 
    """

    def split(string):
        """
        split string into blocks
        """
        length = randint(3,4)
        return [string[i : i + length] for i in range(0, len(string), length)]

    digits = ""
    uppers = ""
    lowers = ""
    others = ""
    for char in password:
        if char.isdigit():
            digits += char
        elif char.isupper():
            uppers += char
        elif char.islower():
            lowers += char
        else:
            others += char

    cluster_list = (
        split(digits) + split(lowers) + split(uppers) + [i for i in others]
    )
    shuffle(cluster_list)

    return "".join(cluster_list)

UPDATE 1:

def banner():
    """
    prepare a banner for use by wallet_main() and wallet_initialize()
    """
    return f"""
    ******************************************
    *** Welcome to CypherVault v{VERSION:.8f} ***
    ******************************************
    """

def wallet_main(passwords):
    """
    1: ENTER A NEW PASSWORD OR EDIT A PASSWORD
    2: DELETE A PASSWORD
    3: SUGGEST A PASSWORD
    4: PRINT SITE/USER LIST
    5: PRINT SITE/USER/PASSWORD LIST
    6: EXIT
    """
    crypto_indite(passwords)
    while True:
        print("\033c", it("green", banner()), it("green", wallet_main.__doc__))
        choice = input("input choice or press Enter to GET A PASSWORD: ") or "0"
        if choice.isdigit():
            choice = int(choice)
            if 0 <= choice <= 6:
                break
        print(f"\033c\n\n\n\tinvalid choice <{it('green', choice)}> try again")
        time.sleep(2)
    menu = {
        0: option_get,
        1: option_post,
        2: option_delete,
        3: option_suggest,
        4: option_print,
        5: option_print_full,
        6: crypto_indite,
    }
    menu[choice](passwords)
    if choice == 6:
        sys.exit()


def wallet_initialize(master):
    """
    initialize password dictionary and prompt for master password
    """
    # read password dictionary if none exists create a new encrypted cyphervault
    # all passwords are in format >>> passwords[site][user]
    print("\033c", it("green", banner()))
    try:
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        assert len(cyphervault) > 0
    except Exception:
        print("\nCypherVault not found, intitializing new...")
        doc_write("cyphervault.txt", (crypto_100() + "$n"))
        passwords = {"master": {"master": "password"}}
        crypto_indite(passwords)
        cyphervault = doc_read("cyphervault.txt").split("$", 1)[1]
        print(it("purple", "\nyour default password is has been set to:"))
        print(it("green", "\npassword\n"))
    if master is None:
        master = getpass("Enter your master password:  ")
    # attempt to decrypt the cyphervault with the supplied password
    decrypted = False
    try:
        passwords = json_loads(crypto_decrypt(cyphervault, master))
        decrypted = True
    except Exception:
        trace()
        print(it("green", "\ninvalid master password, press Enter to try again..."))
        input("\npress Enter to return to main menu")
        wallet_initialize(master=None)  # recursion
    if decrypted:
        print(it("green", "\n    login successful!"))
        # warn if password is default
        if master == "password":
            print(it("purple", "\nyou should change default password immediately!"))
            print("\nyour master password is: ", it("green", master))
        # perform some tests on the password
        audit(master)
        wallet_main(passwords)

if __name__ == "__main__":
    wallet_initialize(master=None)
missed one line `clip_set("")`
Source Link
def read_only():
    """
    require sudo to edit CypherVault.py
    """
    PATH = str(os.path.dirname(os.path.abspath(__file__))) + "/"
    os.chmod(PATH+"cyphervault.py", S_IREAD)
    
def pycryptodome():
    """
    \nTEST FAILED!\n\n
    "pycrypto" is not maintained and has been replaced by "pycryptodome"\n
    packages installed on your system:\n
    """
    print("\033c\n\nensuring", it("green","pycryptodome"), "is installed...")   
    print("ensuring pycrypto is", it("purple","NOT"), "installed...")
    p = Popen(["pip", "show", "pycrypto"], stdout=PIPE)
    out_pycrypto, _ = p.communicate()
    p = Popen(["pip", "show", "pycryptodome"], stdout=PIPE)
    out_pycryptodome, _ = p.communicate()
    out_pycrypto = out_pycrypto.decode()
    out_pycryptodome = out_pycryptodome.decode()
    if (("Version" in out_pycrypto) or not ("Version" in out_pycryptodome)):
        print(it("purple", pycryptodome.__doc__), out_pycryptodome, out_pycrypto)
        raise ValueError(it("green", "pip3 install pycryptodome"))
    else:
        print(it("purple", "\n\nTEST PASSED!\n\n"))
        time.sleep(1)
    
def sites_without(passwords):
    """
    maintain a list of user accounts in JSON format in CypherVault_accounts.txt
    """
    accounts = {k:{k2:"" for k2,v2 in v.items()} for k,v in passwords.items()}
    doc_write(
        "CypherVault_accounts.txt",
        json_dumps(accounts, indent=0, sort_keys=True)
    )

def option_import_json(passwords):
    """
    use this utility to import a password dictionary
    via cut and paste from text editor
    it must be properly formatted JSON, example:\n
    {"site_name":{"user_name":"your_password"}}\n
    which is the same format as menu choice\n
    PRINT SITE/USER/PASSWORD LIST JSON\n
    for email addresses site and user must match\n
    DO NOT save the text document!
    use an "untitled" text document,
    so it does not autosave and remains in RAM!\n
    to skip and return to main menu, press Enter\n 
    """
    print("\033c\n\n\n", it("green", option_import_json.__doc__))
    json_doc = input("enter your JSON formatted password list:  ")
    if json_doc:
        try:
            dictionary = json_loads(json_doc)
            try:
                d_is = isinstance(dictionary, dict)
                for k, v in dictionary.items():
                    k_is = isinstance(k, str)
                    v_is = isinstance(v, dict)
                    for k2, v2 in v.items():
                        k2_is = isinstance(k, str)
                        v2_is = isinstance(k, str)
                valid = d_is and k_is and v_is and k2_is and v2_is
                if not valid:
                    raise ValueError
            except ValueError:
                trace()
                msg = """
                    improperly formatted passwords dictionary\n\n
                    press Enter to return to main menu
                    """
                input(msg)
                wallet_main(passwords)
            passwords.update(dictionary)
            crypto_indite(passwords)
            print(it("green", "\nCypherVault successfully updated!"))
            wallet_main(passwords)
        except ValueError:
            trace()
            input("invalid json\n\npress Enter to return to main menu")
    clip_set("")
    wallet_main(passwords)

def cluster(password):
    """
    format password into blocks of numbers, letters, and symbols 
    """

    def split(string):
        """
        split string into blocks
        """
        length = randint(3,4)
        return [string[i : i + length] for i in range(0, len(string), length)]

    digits = ""
    uppers = ""
    lowers = ""
    others = ""
    for char in password:
        if char.isdigit():
            digits += char
        elif char.isupper():
            uppers += char
        elif char.islower():
            lowers += char
        else:
            others += char

    cluster_list = (
        split(digits) + split(lowers) + split(uppers) + [i for i in others]
    )
    shuffle(cluster_list)

    return "".join(cluster_list)
def read_only():
    """
    require sudo to edit CypherVault.py
    """
    PATH = str(os.path.dirname(os.path.abspath(__file__))) + "/"
    os.chmod(PATH+"cyphervault.py", S_IREAD)
    
def pycryptodome():
    """
    \nTEST FAILED!\n\n
    "pycrypto" is not maintained and has been replaced by "pycryptodome"\n
    packages installed on your system:\n
    """
    print("\033c\n\nensuring", it("green","pycryptodome"), "is installed...")   
    print("ensuring pycrypto is", it("purple","NOT"), "installed...")
    p = Popen(["pip", "show", "pycrypto"], stdout=PIPE)
    out_pycrypto, _ = p.communicate()
    p = Popen(["pip", "show", "pycryptodome"], stdout=PIPE)
    out_pycryptodome, _ = p.communicate()
    out_pycrypto = out_pycrypto.decode()
    out_pycryptodome = out_pycryptodome.decode()
    if (("Version" in out_pycrypto) or not ("Version" in out_pycryptodome)):
        print(it("purple", pycryptodome.__doc__), out_pycryptodome, out_pycrypto)
        raise ValueError(it("green", "pip3 install pycryptodome"))
    else:
        print(it("purple", "\n\nTEST PASSED!\n\n"))
        time.sleep(1)
    
def sites_without(passwords):
    """
    maintain a list of user accounts in JSON format in CypherVault_accounts.txt
    """
    accounts = {k:{k2:"" for k2,v2 in v.items()} for k,v in passwords.items()}
    doc_write(
        "CypherVault_accounts.txt",
        json_dumps(accounts, indent=0, sort_keys=True)
    )

def option_import_json(passwords):
    """
    use this utility to import a password dictionary
    via cut and paste from text editor
    it must be properly formatted JSON, example:\n
    {"site_name":{"user_name":"your_password"}}\n
    which is the same format as menu choice\n
    PRINT SITE/USER/PASSWORD LIST JSON\n
    for email addresses site and user must match\n
    DO NOT save the text document!
    use an "untitled" text document,
    so it does not autosave and remains in RAM!\n
    to skip and return to main menu, press Enter\n 
    """
    print("\033c\n\n\n", it("green", option_import_json.__doc__))
    json_doc = input("enter your JSON formatted password list:  ")
    if json_doc:
        try:
            dictionary = json_loads(json_doc)
            try:
                d_is = isinstance(dictionary, dict)
                for k, v in dictionary.items():
                    k_is = isinstance(k, str)
                    v_is = isinstance(v, dict)
                    for k2, v2 in v.items():
                        k2_is = isinstance(k, str)
                        v2_is = isinstance(k, str)
                valid = d_is and k_is and v_is and k2_is and v2_is
                if not valid:
                    raise ValueError
            except ValueError:
                trace()
                msg = """
                    improperly formatted passwords dictionary\n\n
                    press Enter to return to main menu
                    """
                input(msg)
                wallet_main(passwords)
            passwords.update(dictionary)
            crypto_indite(passwords)
            print(it("green", "\nCypherVault successfully updated!"))
            wallet_main(passwords)
        except ValueError:
            trace()
            input("invalid json\n\npress Enter to return to main menu")
    wallet_main(passwords)

def cluster(password):
    """
    format password into blocks of numbers, letters, and symbols 
    """

    def split(string):
        """
        split string into blocks
        """
        length = randint(3,4)
        return [string[i : i + length] for i in range(0, len(string), length)]

    digits = ""
    uppers = ""
    lowers = ""
    others = ""
    for char in password:
        if char.isdigit():
            digits += char
        elif char.isupper():
            uppers += char
        elif char.islower():
            lowers += char
        else:
            others += char

    cluster_list = (
        split(digits) + split(lowers) + split(uppers) + [i for i in others]
    )
    shuffle(cluster_list)

    return "".join(cluster_list)
def read_only():
    """
    require sudo to edit CypherVault.py
    """
    PATH = str(os.path.dirname(os.path.abspath(__file__))) + "/"
    os.chmod(PATH+"cyphervault.py", S_IREAD)
    
def pycryptodome():
    """
    \nTEST FAILED!\n\n
    "pycrypto" is not maintained and has been replaced by "pycryptodome"\n
    packages installed on your system:\n
    """
    print("\033c\n\nensuring", it("green","pycryptodome"), "is installed...")   
    print("ensuring pycrypto is", it("purple","NOT"), "installed...")
    p = Popen(["pip", "show", "pycrypto"], stdout=PIPE)
    out_pycrypto, _ = p.communicate()
    p = Popen(["pip", "show", "pycryptodome"], stdout=PIPE)
    out_pycryptodome, _ = p.communicate()
    out_pycrypto = out_pycrypto.decode()
    out_pycryptodome = out_pycryptodome.decode()
    if (("Version" in out_pycrypto) or not ("Version" in out_pycryptodome)):
        print(it("purple", pycryptodome.__doc__), out_pycryptodome, out_pycrypto)
        raise ValueError(it("green", "pip3 install pycryptodome"))
    else:
        print(it("purple", "\n\nTEST PASSED!\n\n"))
        time.sleep(1)
    
def sites_without(passwords):
    """
    maintain a list of user accounts in JSON format in CypherVault_accounts.txt
    """
    accounts = {k:{k2:"" for k2,v2 in v.items()} for k,v in passwords.items()}
    doc_write(
        "CypherVault_accounts.txt",
        json_dumps(accounts, indent=0, sort_keys=True)
    )

def option_import_json(passwords):
    """
    use this utility to import a password dictionary
    via cut and paste from text editor
    it must be properly formatted JSON, example:\n
    {"site_name":{"user_name":"your_password"}}\n
    which is the same format as menu choice\n
    PRINT SITE/USER/PASSWORD LIST JSON\n
    for email addresses site and user must match\n
    DO NOT save the text document!
    use an "untitled" text document,
    so it does not autosave and remains in RAM!\n
    to skip and return to main menu, press Enter\n 
    """
    print("\033c\n\n\n", it("green", option_import_json.__doc__))
    json_doc = input("enter your JSON formatted password list:  ")
    if json_doc:
        try:
            dictionary = json_loads(json_doc)
            try:
                d_is = isinstance(dictionary, dict)
                for k, v in dictionary.items():
                    k_is = isinstance(k, str)
                    v_is = isinstance(v, dict)
                    for k2, v2 in v.items():
                        k2_is = isinstance(k, str)
                        v2_is = isinstance(k, str)
                valid = d_is and k_is and v_is and k2_is and v2_is
                if not valid:
                    raise ValueError
            except ValueError:
                trace()
                msg = """
                    improperly formatted passwords dictionary\n\n
                    press Enter to return to main menu
                    """
                input(msg)
                wallet_main(passwords)
            passwords.update(dictionary)
            crypto_indite(passwords)
            print(it("green", "\nCypherVault successfully updated!"))
            wallet_main(passwords)
        except ValueError:
            trace()
            input("invalid json\n\npress Enter to return to main menu")
    clip_set("")
    wallet_main(passwords)

def cluster(password):
    """
    format password into blocks of numbers, letters, and symbols 
    """

    def split(string):
        """
        split string into blocks
        """
        length = randint(3,4)
        return [string[i : i + length] for i in range(0, len(string), length)]

    digits = ""
    uppers = ""
    lowers = ""
    others = ""
    for char in password:
        if char.isdigit():
            digits += char
        elif char.isupper():
            uppers += char
        elif char.islower():
            lowers += char
        else:
            others += char

    cluster_list = (
        split(digits) + split(lowers) + split(uppers) + [i for i in others]
    )
    shuffle(cluster_list)

    return "".join(cluster_list)
added 5793 characters in body
Source Link
Loading
referenced pycryptodome explicitly, not its deprecated equal pycrypto which is sometimes installed by mistake
Source Link
Loading
removed some line breaks
Source Link
Loading
Tweeted twitter.com/StackCodeReview/status/1210802686439784448
Became Hot Network Question
added 23 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 8 characters in body
Source Link
Loading
Source Link
Loading