#!/usr/bin/env python3 # Configuration! spacebar = "1rzys0" keys = "4bald5hozx8f6veqrp0mw13s9njktyug7ci2" # The code # Do not edit below, change at your own peril from typing import List, Dict from sys import argv from random import randrange from os.path import exists try: from pyperclip import copy as set_clipboard except ImportError: print("Missing 'pyperclip' module") print("This probably means you forgot to make and activate a virtual environment") print() print("1. Make Venv: python3 -m venv env") print("2. Activate: . env/bin/activate (Use env/Scripts/activate.bat for Windows)") print("3. Install module: pip install -r requirements.txt") print() exit() basic: str = "abcdefghijklmnopqrstuvwxyz0123456789" def gen_card() -> Dict[str, str]: c: Dict[str, str] = {} for i in range(len(keys)): b = basic[i] k = keys[i] c[b] = k c["."] = "" return c def modify_self(what: str, data: str): old = [] with open("card.py", "r") as f: for line in f: if line.startswith(what): old.append(f"{what} = \"{data.strip()}\"\n") else: old.append(line) with open("card.py", "w") as f: for line in old: f.write(line) def modify_skript(spacebar: str, card: str): old = [] if not exists("skript.js"): return False with open("skript.js", "r") as f: for line in f: if line.startswith("let spacebar"): old.append(f"let spacebar = \"{spacebar.strip()}\";\n") elif line.startswith("let keys"): old.append(f"let keys = \"{card.strip()}\".split(\"\");\n") else: old.append(line) with open(".user.js", "w") as f: for line in old: f.write(line) return True if len(argv) == 1: print("Key.Card v1.0") print("Innovative and easy password generation") print() print("Usage:") print(" card.py Generate a password for given text (Doesn't have to be a domain)") print(" card.py -g Tumbles 'spacebar' and 'keys', used to scramble passwords generated") print(" card.py -r Restores from previous backup (If able)") print() exit() if argv[1] == "card.py": print("Appears the arguments are out of order") print() print("Please report this as an issue with you're platform") print("(i.e. '[bug] Args out of order / Windows')") print() print("https://git.red-green.com/david/Key.Card/issues/new") print() exit() if argv[1] == "-g" or argv[1] == "-G": print("Taking backup of old keys") with open(".keys.old", "w") as f: f.write(keys) print("Taking backup of old spacebar") with open(".spacebar.old", "w") as f: f.write(spacebar) print("Tumbling new keys") picked = "" letter_len = len(basic) while len(picked) < letter_len: pick = basic[randrange(0, letter_len)] if pick in picked: continue picked += pick keys = picked modify_self("keys", keys) print("Tumbling new spacebar") picked = "" while len(picked) < 6: pick = basic[randrange(0, letter_len)] if pick in picked: continue picked += pick spacebar = picked modify_self("spacebar", spacebar) modify_skript(spacebar, keys) print("Done") exit() if argv[1] == "-r" or argv[1] == "-R": if not exists(".keys.old"): print("No old keys file found") if not exists(".spacebar.old"): print("No old spacebar file found") if not exists(".keys.old") and not exists(".spacebar.old"): print() exit() if exists(".keys.old"): print("Restoring keys") with open(".keys.old", "r") as f: keys = f.readline() modify_self("keys", keys) if exists(".spacebar.old"): print("Restoring spacebar") with open(".spacebar.old", "r") as f: spacebar = f.readline() modify_self("spacebar", spacebar) if exists(".keys.old") and exists(".spacebar.old"): modify_skript(spacebar, keys) print("Done") exit() card = gen_card() domain = "".join(argv[1:]).lower() _passwd = "" + spacebar for i in domain: _passwd += card[i] or "" set_clipboard(_passwd) print(f"Generated password for '{domain}'")