From 21d5eed71932e92b1464ba0577c4e54ae68dbe25 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sun, 15 May 2016 23:02:05 +0300 Subject: [PATCH] db and settings encryption --- src/history.py | 21 +++++++++++++++++++++ src/profile.py | 1 + src/settings.py | 6 ++++++ 3 files changed, 28 insertions(+) diff --git a/src/history.py b/src/history.py index 980d146..6fce8bb 100644 --- a/src/history.py +++ b/src/history.py @@ -2,6 +2,8 @@ from sqlite3 import connect import settings from os import chdir +import os.path +from toxencryptsave import LibToxEncryptSave PAGE_SIZE = 42 @@ -17,6 +19,15 @@ class History(object): def __init__(self, name): self._name = name chdir(settings.ProfileHelper.get_path()) + path = settings.ProfileHelper.get_path() + self._name + '.hstr' + if os.path.exists(path): + decr = LibToxEncryptSave.get_instance() + if decr.has_password(): + with open(path, 'rb') as fin: + data = fin.read() + data = decr.pass_decrypt(data) + with open(path, 'wb') as fout: + fout.write(data) db = connect(name + '.hstr') cursor = db.cursor() cursor.execute('CREATE TABLE IF NOT EXISTS friends(' @@ -24,6 +35,16 @@ class History(object): ')') db.close() + def save(self): + encr = LibToxEncryptSave.get_instance() + if encr.has_password(): + path = settings.ProfileHelper.get_path() + self._name + '.hstr' + with open(path, 'rb') as fin: + data = fin.read() + data = encr.pass_encrypt(data) + with open(path, 'wb') as fout: + fout.write(data) + def export(self, directory): path = settings.ProfileHelper.get_path() + self._name + '.hstr' new_path = directory + self._name + '.hstr' diff --git a/src/profile.py b/src/profile.py index 851f959..43c6cd9 100644 --- a/src/profile.py +++ b/src/profile.py @@ -580,6 +580,7 @@ class Profile(Contact, Singleton): if not self._history.friend_exists_in_db(friend.tox_id): self._history.add_friend_to_db(friend.tox_id) self._history.save_messages_to_db(friend.tox_id, messages) + self._history.save() del self._history def clear_history(self, num=None): diff --git a/src/settings.py b/src/settings.py index 5ec3de7..631bfd7 100644 --- a/src/settings.py +++ b/src/settings.py @@ -18,6 +18,9 @@ class Settings(Singleton, dict): if os.path.isfile(self.path): with open(self.path) as fl: data = fl.read() + inst = LibToxEncryptSave.get_instance() + if inst.has_password(): + data = inst.pass_decrypt(data) try: info = json.loads(data) except Exception as ex: @@ -97,6 +100,9 @@ class Settings(Singleton, dict): def save(self): text = json.dumps(self) + inst = LibToxEncryptSave.get_instance() + if inst.has_password(): + text = inst.pass_encrypt(text) with open(self.path, 'w') as fl: fl.write(text)