Last active
May 27, 2020 17:09
-
-
Save MLutt/4b50cdd1644564307bda0d276e235ab4 to your computer and use it in GitHub Desktop.
GnuPG encrypted data storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import pickle | |
import subprocess | |
if __name__ == "__main__": | |
def dictString(dictionary: dict, level=0, seperator=">", whiteSpaceAfterSeperator=True, globalPrefix="") -> str: | |
ret = "" | |
for v in dictionary.keys(): | |
ret += globalPrefix + seperator * level | |
if whiteSpaceAfterSeperator and level > 0: | |
ret += " " | |
if isinstance(dictionary[v], dict): | |
ret += f"{v}\n{dictString(dictionary[v], level + 1, seperator, whiteSpaceAfterSeperator, globalPrefix)}" | |
else: | |
ret += f"{v}: {dictionary[v]}\n" | |
return ret | |
cmd = sys.argv[1].lower() if len(sys.argv) > 1 else "" | |
if cmd in ["edit", "known", "remove", "show"]: | |
if os.path.exists("Data"): | |
gpgDecryption = subprocess.Popen( | |
["gpg", "--quiet", "--decrypt", "Data"], | |
stdout=subprocess.PIPE, | |
bufsize=0 | |
) | |
try: | |
KeyData = pickle.load(gpgDecryption.stdout) | |
except EOFError: | |
print("Could not parse data dictionary (EOFError) - did the decryption fail?") | |
sys.exit(601) | |
if not isinstance(KeyData, dict): | |
print("Malformed data!") | |
sys.exit(666) | |
else: | |
KeyData = {} | |
else: | |
print("Unsupported command execution.") | |
sys.exit(7) | |
if cmd == "edit": | |
tree = input("Please specify the card to edit/add/overwrite: ") | |
dummy = {} | |
dummy["PIV"] = {} | |
dummy["Challenge"] = {} | |
dummy["GPG"] = {} | |
dummy["Serial"] = input("SERIAL: ") | |
dummy["PIV"]["Management"] = input("PIV->Management: ") | |
dummy["PIV"]["InitialPIN"] = input("PIV->InitialPIN: ") | |
dummy["Challenge"]["Slot1"] = input("Challenge->Slot1: ") | |
dummy["Challenge"]["Slot2"] = input("Challenge->Slot2: ") | |
dummy["GPG"]["AdminPIN"] = input("GPG->AdminPIN: ") | |
dummy["GPG"]["ResetCode"] = input("GPG->ResetCode: ") | |
KeyData[tree] = dummy | |
elif cmd == "known": | |
print(f"The following keys are known: {KeyData.keys()}") | |
sys.exit(0) | |
elif cmd == "remove": | |
tree = input("Please insert the card name to remove: ") | |
del KeyData[tree] | |
elif cmd == "show": | |
try: | |
print(dictString(KeyData[input("Which key do you want to display? ")], globalPrefix="-")) | |
except KeyError: | |
print("The specified key was not found. You can get the list of known keys with the \"known\" command.") | |
sys.exit(0) | |
if os.path.exists("Data"): | |
os.remove("Data") | |
if os.path.exists("Data.sig"): | |
os.remove("Data.sig") | |
gpgEncryption = subprocess.Popen( | |
["gpg", "--quiet", "--output", "Data", "--encrypt", "--recipient", "[email protected]"], | |
stdin=subprocess.PIPE, | |
bufsize=0 | |
) | |
pickle.dump(KeyData, gpgEncryption.stdin) | |
gpgEncryption.stdin.close() | |
os.system("gpg --detach-sign Data") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment