card.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python3
  2. # Configuration!
  3. spacebar = "36a5my"
  4. keys = "i08rljtyhp2bkexoaf3w7946ncugd51qszmv"
  5. # The code
  6. # Do not edit below, change at your own peril
  7. from typing import List, Dict
  8. from sys import argv
  9. from random import randrange
  10. from os.path import exists
  11. try:
  12. from pyperclip import copy as set_clipboard
  13. except ImportError:
  14. print("Missing 'pyperclip' module")
  15. print("This probably means you forgot to make and activate a virtual environment")
  16. print()
  17. print("1. Make Venv: python3 -m venv env")
  18. print("2. Activate: . env/bin/activate (Use env/Scripts/activate.bat for Windows)")
  19. print("3. Install module: pip install -r requirements.txt")
  20. print()
  21. exit()
  22. basic: str = "abcdefghijklmnopqrstuvwxyz0123456789"
  23. def gen_card() -> Dict[str, str]:
  24. c: Dict[str, str] = {}
  25. for i in range(len(keys)):
  26. b = basic[i]
  27. k = keys[i]
  28. c[b] = k
  29. c["."] = ""
  30. return c
  31. def modify_self(what: str, data: str):
  32. old = []
  33. with open("card.py", "r") as f:
  34. for line in f:
  35. if line.startswith(what):
  36. old.append(f"{what} = \"{data}\"\n")
  37. else:
  38. old.append(line)
  39. with open("card.py", "w") as f:
  40. for line in old:
  41. f.write(line)
  42. def modify_skript(spacebar, card):
  43. old = []
  44. if not exists("skript.js"):
  45. return False
  46. with open("skript.js", "r") as f:
  47. for line in f:
  48. if line.startswith("let spacebar"):
  49. old.append(f"let spacebar = \"{spacebar}\";\n")
  50. elif line.startswith("let keys"):
  51. old.append(f"let keys = \"{card}\".split(\"\");\n")
  52. else:
  53. old.append(line)
  54. with open(".user.js", "w") as f:
  55. for line in old:
  56. f.write(line)
  57. return True
  58. if len(argv) == 1:
  59. print("Key.Card v1.0")
  60. print("Innovative and easy password generation")
  61. print()
  62. print("Usage:")
  63. print(" card.py <domain.com> Generate a password for given text (Doesn't have to be a domain)")
  64. print(" card.py -g Tumbles 'spacebar' and 'keys', used to scramble passwords generated")
  65. print(" card.py -r Restores from previous backup (If able)")
  66. print()
  67. exit()
  68. if argv[1] == "card.py":
  69. print("Appears the arguments are out of order")
  70. print()
  71. print("Please report this as an issue with you're platform")
  72. print("(i.e. '[bug] Args out of order / Windows')")
  73. print()
  74. print("https://git.red-green.com/david/Key.Card/issues/new")
  75. print()
  76. exit()
  77. if argv[1] == "-g" or argv[1] == "-G":
  78. print("Taking backup of old keys")
  79. with open(".keys.old", "w") as f:
  80. f.write(keys)
  81. print("Taking backup of old spacebar")
  82. with open(".spacebar.old", "w") as f:
  83. f.write(spacebar)
  84. print("Tumbling new keys")
  85. picked = ""
  86. letter_len = len(basic)
  87. while len(picked) < letter_len:
  88. pick = basic[randrange(0, letter_len)]
  89. if pick in picked:
  90. continue
  91. picked += pick
  92. keys = picked
  93. modify_self("keys", keys)
  94. print("Tumbling new spacebar")
  95. picked = ""
  96. while len(picked) < 6:
  97. pick = basic[randrange(0, letter_len)]
  98. if pick in picked:
  99. continue
  100. picked += pick
  101. spacebar = picked
  102. modify_self("spacebar", spacebar)
  103. modify_skript(spacebar, keys)
  104. print("Done")
  105. exit()
  106. if argv[1] == "-r" or argv[1] == "-R":
  107. if not exists(".keys.old"):
  108. print("No old keys file found")
  109. if not exists(".spacebar.old"):
  110. print("No old spacebar file found")
  111. if not exists(".keys.old") and not exists(".spacebar.old"):
  112. print()
  113. exit()
  114. if exists(".keys.old"):
  115. print("Restoring keys")
  116. with open(".keys.old", "r") as f:
  117. keys = f.readline()
  118. modify_self("keys", keys)
  119. if exists(".spacebar.old"):
  120. print("Restoring spacebar")
  121. with open(".spacebar.old", "r") as f:
  122. spacebar = f.readline()
  123. modify_self("spacebar", spacebar)
  124. if exists(".keys.old") and exists(".spacebar.old"):
  125. modify_skript(spacebar, keys)
  126. print("Done")
  127. exit()
  128. card = gen_card()
  129. domain = "".join(argv[1:]).lower()
  130. _passwd = "" + spacebar
  131. for i in domain:
  132. _passwd += card[i] or "?"
  133. set_clipboard(_passwd)
  134. print(f"Generated password for '{domain}'")