From 94c1368f72b1ccf87407642e1af9a24cb64c0ff4 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 9 Jan 2018 16:58:12 -0600 Subject: [PATCH] finished encrypt/decryption of data --- core.py | 5 ++++- onionr.py | 23 +++++++++++++++++++---- onionrutils.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 onionrutils.py diff --git a/core.py b/core.py index a0909067..baf56d68 100644 --- a/core.py +++ b/core.py @@ -52,9 +52,12 @@ class Core: try: decrypted = simplecrypt.decrypt(password, data) except simplecrypt.DecryptionException: - return (False, 'wrong password') + return (False, 'wrong password (or corrupted archive)') else: open('data.tar', 'wb').write(decrypted) + tar = tarfile.open('data.tar') + tar.extractall() + tar.close() return (True, '') def daemonQueue(self): # This function intended to be used by the client diff --git a/onionr.py b/onionr.py index 4ec4ba90..f4928752 100755 --- a/onionr.py +++ b/onionr.py @@ -14,8 +14,9 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import sys, os, threading, configparser, base64, random -import gui, api, colors +import sys, os, threading, configparser, base64, random, getpass, shutil +import gui, api, colors, core +from onionrutils import OnionrUtils from colors import Colors class Onionr: @@ -23,13 +24,24 @@ class Onionr: colors = Colors() + onionrCore = core.Core() + onionrUtils = OnionrUtils() + # Get configuration and Handle commands self.debug = False # Whole application debugging os.chdir(sys.path[0]) - - if not os.path.exists('data'): + if os.path.exists('data-encrypted.dat'): + while True: + print('Enter password to decrypt:') + password = getpass.getpass() + result = onionrCore.dataDirDecrypt(password) + if os.path.exists('data/'): + break + else: + print('Failed to decrypt: ' + result[1]) + else: os.mkdir('data') # Get configuration @@ -65,6 +77,9 @@ class Onionr: else: print(colors.RED, 'Invalid Command', colors.RESET) return + encryptionPassword = onionrUtils.getPassword('Enter password to encrypt directory.') + onionrCore.dataDirEncrypt(encryptionPassword) + shutil.rmtree('data/') return def daemon(self): os.system('./communicator.py') diff --git a/onionrutils.py b/onionrutils.py new file mode 100644 index 00000000..d813575f --- /dev/null +++ b/onionrutils.py @@ -0,0 +1,33 @@ +''' + Onionr - P2P Microblogging Platform & Social network + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +''' +# Misc functions that do not fit in the main api, but are useful +import getpass +class OnionrUtils(): + def __init__(self): + return + def getPassword(self, message='Enter password: '): + # Get a password safely with confirmation and return it + while True: + print(message) + pass1 = getpass.getpass() + print('Confirm password: ') + pass2 = getpass.getpass() + if pass1 != pass2: + print("Passwords do not match.") + input() + else: + break + return pass1 \ No newline at end of file