From 825d8eb647acdb0415ad9001f4c4899d085dbe9a Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 6 Sep 2020 08:07:15 +0300 Subject: [PATCH 1/2] Block overwrite --- rsa.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/rsa.py b/rsa.py index a46d885..320560f 100755 --- a/rsa.py +++ b/rsa.py @@ -30,7 +30,12 @@ def main(): print("hesham-rsa version ", VERSION) if sys.argv[1] == "gen" and len(sys.argv) == 4: ##rsa gen keyFileName = sys.argv[3] - key = generateKeys(keyFileName, int(sys.argv[2])) + if keyExist(keyFileName): + ##ask + print("key already exists") + sys.exit(0) + else: + key = generateKeys(keyFileName, int(sys.argv[2])) print("e: ", key[E]) print("n: ", key[N]) print("d: ", key[D]) @@ -351,5 +356,8 @@ def backTrack(x): print("\b",end="") length -= 1 +def keyExist(keyName): + exist = os.path.exists(keysFolder+keyName) + return exist if __name__ == "__main__": main() From a864749812881024ac2c37ab853aa6a489547aa2 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 6 Sep 2020 08:51:54 +0300 Subject: [PATCH 2/2] key-overwrite: prompt overwrite choice. --- rsa.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rsa.py b/rsa.py index 320560f..d534067 100755 --- a/rsa.py +++ b/rsa.py @@ -31,9 +31,14 @@ def main(): if sys.argv[1] == "gen" and len(sys.argv) == 4: ##rsa gen keyFileName = sys.argv[3] if keyExist(keyFileName): - ##ask - print("key already exists") - sys.exit(0) + choice = input("overwrite key %s (y/n)" % keyFileName) + if choice == "y": + key = generateKeys(keyFileName, int(sys.argv[2])) + elif choice == "n": + sys.exit(0) + else: + print("unrecognized choice!") + sys.exit(1) else: key = generateKeys(keyFileName, int(sys.argv[2])) print("e: ", key[E])