db and settings encryption

This commit is contained in:
ingvar1995 2016-05-15 23:02:05 +03:00
parent 0fa7ca7b5c
commit 21d5eed719
3 changed files with 28 additions and 0 deletions

View file

@ -2,6 +2,8 @@
from sqlite3 import connect from sqlite3 import connect
import settings import settings
from os import chdir from os import chdir
import os.path
from toxencryptsave import LibToxEncryptSave
PAGE_SIZE = 42 PAGE_SIZE = 42
@ -17,6 +19,15 @@ class History(object):
def __init__(self, name): def __init__(self, name):
self._name = name self._name = name
chdir(settings.ProfileHelper.get_path()) 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') db = connect(name + '.hstr')
cursor = db.cursor() cursor = db.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS friends(' cursor.execute('CREATE TABLE IF NOT EXISTS friends('
@ -24,6 +35,16 @@ class History(object):
')') ')')
db.close() 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): def export(self, directory):
path = settings.ProfileHelper.get_path() + self._name + '.hstr' path = settings.ProfileHelper.get_path() + self._name + '.hstr'
new_path = directory + self._name + '.hstr' new_path = directory + self._name + '.hstr'

View file

@ -580,6 +580,7 @@ class Profile(Contact, Singleton):
if not self._history.friend_exists_in_db(friend.tox_id): if not self._history.friend_exists_in_db(friend.tox_id):
self._history.add_friend_to_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_messages_to_db(friend.tox_id, messages)
self._history.save()
del self._history del self._history
def clear_history(self, num=None): def clear_history(self, num=None):

View file

@ -18,6 +18,9 @@ class Settings(Singleton, dict):
if os.path.isfile(self.path): if os.path.isfile(self.path):
with open(self.path) as fl: with open(self.path) as fl:
data = fl.read() data = fl.read()
inst = LibToxEncryptSave.get_instance()
if inst.has_password():
data = inst.pass_decrypt(data)
try: try:
info = json.loads(data) info = json.loads(data)
except Exception as ex: except Exception as ex:
@ -97,6 +100,9 @@ class Settings(Singleton, dict):
def save(self): def save(self):
text = json.dumps(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: with open(self.path, 'w') as fl:
fl.write(text) fl.write(text)