Explorar o código

Updated for a local 'card.py' CLI script

apollo hai 1 semana
pai
achega
fe4280c19b
Modificáronse 4 ficheiros con 160 adicións e 0 borrados
  1. 2 0
      .gitignore
  2. 10 0
      README.md
  3. 147 0
      card.py
  4. 1 0
      requirements.txt

+ 2 - 0
.gitignore

@@ -1 +1,3 @@
 .user.js
+env/
+*.old

+ 10 - 0
README.md

@@ -2,6 +2,8 @@
 
 A user script to generate passwords based on Qwerty Card
 
+> Now with a *local* python script `card.py`
+
 ## Installation
 
 > I'll assume you have [Greasemonkey](https://www.greasespot.net/) already installed
@@ -18,3 +20,11 @@ A user script to generate passwords based on Qwerty Card
     4. Press CTRL + S to save.
     5. Reload open pages to see it run.
 
+## Using the *local* card.py
+
+> I now have a local python3 script you can run to get the same effect as using you're grease monkey script (This way you have a CLI to use)
+
+1. Clone this repo
+2. Make a python virtual environment (See [venv](https://docs.python.org/3/library/venv.html))
+3. (Activate virtual environment) `pip install pyperclip` (See [pyperclip](https://pypi.org/project/pyperclip/) for additional setup it may require, *Under Linux*)
+4. Run `./card.py` (This should display help)

+ 147 - 0
card.py

@@ -0,0 +1,147 @@
+#!/usr/bin/env python3
+
+# Configuration!
+spacebar = "36a5my"
+keys = "i08rljtyhp2bkexoaf3w7946ncugd51qszmv"
+
+# 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}\"\n")
+            else:
+                old.append(line)
+    with open("card.py", "w") as f:
+        for line in old:
+            f.write(line)
+
+def modify_skript(spacebar, card):
+    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}\";\n")
+            elif line.startswith("let keys"):
+                old.append(f"let keys = \"{card}\".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 <domain.com>         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}'")

+ 1 - 0
requirements.txt

@@ -0,0 +1 @@
+pyperclip==1.11.0