toxes refactoring

This commit is contained in:
ingvar1995 2017-02-11 20:07:28 +03:00
parent 481e48f495
commit 3511031aff
10 changed files with 86 additions and 71 deletions

View file

@ -2,7 +2,7 @@ from sqlite3 import connect
import settings import settings
from os import chdir from os import chdir
import os.path import os.path
from toxencryptsave import ToxEncryptSave from toxes import ToxES
PAGE_SIZE = 42 PAGE_SIZE = 42
@ -25,7 +25,7 @@ class History:
chdir(settings.ProfileHelper.get_path()) chdir(settings.ProfileHelper.get_path())
path = settings.ProfileHelper.get_path() + self._name + '.hstr' path = settings.ProfileHelper.get_path() + self._name + '.hstr'
if os.path.exists(path): if os.path.exists(path):
decr = ToxEncryptSave.get_instance() decr = ToxES.get_instance()
try: try:
with open(path, 'rb') as fin: with open(path, 'rb') as fin:
data = fin.read() data = fin.read()
@ -43,7 +43,7 @@ class History:
db.close() db.close()
def save(self): def save(self):
encr = ToxEncryptSave.get_instance() encr = ToxES.get_instance()
if encr.has_password(): if encr.has_password():
path = settings.ProfileHelper.get_path() + self._name + '.hstr' path = settings.ProfileHelper.get_path() + self._name + '.hstr'
with open(path, 'rb') as fin: with open(path, 'rb') as fin:
@ -57,7 +57,7 @@ class History:
new_path = directory + self._name + '.hstr' new_path = directory + self._name + '.hstr'
with open(path, 'rb') as fin: with open(path, 'rb') as fin:
data = fin.read() data = fin.read()
encr = ToxEncryptSave.get_instance() encr = ToxES.get_instance()
if encr.has_password(): if encr.has_password():
data = encr.pass_encrypt(data) data = encr.pass_encrypt(data)
with open(new_path, 'wb') as fout: with open(new_path, 'wb') as fout:

View file

@ -12,7 +12,7 @@ from callbacks import init_callbacks, stop, start
from util import curr_directory, program_version, remove, is_64_bit from util import curr_directory, program_version, remove, is_64_bit
import styles.style import styles.style
import platform import platform
import toxencryptsave import toxes
from passwordscreen import PasswordScreen, UnlockAppScreen, SetProfilePasswordScreen from passwordscreen import PasswordScreen, UnlockAppScreen, SetProfilePasswordScreen
from plugin_support import PluginLoader from plugin_support import PluginLoader
import updater import updater
@ -37,7 +37,7 @@ class Toxygen:
Show password screen Show password screen
""" """
tmp = [data] tmp = [data]
p = PasswordScreen(toxencryptsave.ToxEncryptSave.get_instance(), tmp) p = PasswordScreen(toxes.ToxES.get_instance(), tmp)
p.show() p.show()
self.app.connect(self.app, QtCore.SIGNAL("lastWindowClosed()"), self.app, QtCore.SLOT("quit()")) self.app.connect(self.app, QtCore.SIGNAL("lastWindowClosed()"), self.app, QtCore.SLOT("quit()"))
self.app.exec_() self.app.exec_()
@ -62,7 +62,7 @@ class Toxygen:
dark_style = fl.read() dark_style = fl.read()
app.setStyleSheet(dark_style) app.setStyleSheet(dark_style)
encrypt_save = toxencryptsave.ToxEncryptSave() encrypt_save = toxes.ToxES()
if self.path is not None: if self.path is not None:
path = os.path.dirname(self.path) + '/' path = os.path.dirname(self.path) + '/'
@ -248,7 +248,7 @@ class Toxygen:
def correct_pass(): def correct_pass():
show() show()
Settings.get_instance().locked = False Settings.get_instance().locked = False
self.p = UnlockAppScreen(toxencryptsave.ToxEncryptSave.get_instance(), correct_pass) self.p = UnlockAppScreen(toxes.ToxES.get_instance(), correct_pass)
self.p.show() self.p.show()
def tray_activated(reason): def tray_activated(reason):

View file

@ -6,6 +6,7 @@ import plugin_support
from mainscreen_widgets import * from mainscreen_widgets import *
import settings import settings
import platform import platform
import toxes
class MainWindow(QtGui.QMainWindow, Singleton): class MainWindow(QtGui.QMainWindow, Singleton):
@ -478,7 +479,7 @@ class MainWindow(QtGui.QMainWindow, Singleton):
msgBox.exec_() msgBox.exec_()
def lock_app(self): def lock_app(self):
if toxencryptsave.ToxEncryptSave.get_instance().has_password(): if toxes.ToxES.get_instance().has_password():
Settings.get_instance().locked = True Settings.get_instance().locked = True
self.hide() self.hide()
else: else:

View file

@ -7,7 +7,7 @@ from profile import Profile
from util import curr_directory, copy from util import curr_directory, copy
from widgets import CenteredWidget, DataLabel, LineEdit from widgets import CenteredWidget, DataLabel, LineEdit
import pyaudio import pyaudio
import toxencryptsave import toxes
import plugin_support import plugin_support
import updater import updater
@ -215,7 +215,7 @@ class ProfileSettings(CenteredWidget):
def new_password(self): def new_password(self):
if self.password.text() == self.confirm_password.text(): if self.password.text() == self.confirm_password.text():
if not len(self.password.text()) or len(self.password.text()) >= 8: if not len(self.password.text()) or len(self.password.text()) >= 8:
e = toxencryptsave.ToxEncryptSave.get_instance() e = toxes.ToxES.get_instance()
e.set_password(self.password.text()) e.set_password(self.password.text())
self.close() self.close()
else: else:

View file

@ -4,7 +4,7 @@ import os
import importlib import importlib
import inspect import inspect
import plugins.plugin_super_class as pl import plugins.plugin_super_class as pl
import toxencryptsave import toxes
import sys import sys
@ -16,7 +16,7 @@ class PluginLoader(util.Singleton):
self._settings = settings self._settings = settings
self._plugins = {} # dict. key - plugin unique short name, value - tuple (plugin instance, is active) self._plugins = {} # dict. key - plugin unique short name, value - tuple (plugin instance, is active)
self._tox = tox self._tox = tox
self._encr = toxencryptsave.ToxEncryptSave.get_instance() self._encr = toxes.ToxES.get_instance()
def set_tox(self, tox): def set_tox(self, tox):
""" """

View file

@ -43,7 +43,7 @@ class PluginSuperClass:
:param tox: tox instance :param tox: tox instance
:param profile: profile instance :param profile: profile instance
:param settings: profile settings :param settings: profile settings
:param encrypt_save: LibToxEncryptSave instance. :param encrypt_save: ToxES instance.
""" """
self._settings = settings self._settings = settings
self._profile = profile self._profile = profile

View file

@ -3,7 +3,7 @@ import json
import os import os
from util import Singleton, curr_directory, log, copy, append_slash from util import Singleton, curr_directory, log, copy, append_slash
import pyaudio import pyaudio
from toxencryptsave import ToxEncryptSave from toxes import ToxES
import smileys import smileys
@ -19,7 +19,7 @@ class Settings(dict, Singleton):
if os.path.isfile(self.path): if os.path.isfile(self.path):
with open(self.path, 'rb') as fl: with open(self.path, 'rb') as fl:
data = fl.read() data = fl.read()
inst = ToxEncryptSave.get_instance() inst = ToxES.get_instance()
try: try:
if inst.is_data_encrypted(data): if inst.is_data_encrypted(data):
data = inst.pass_decrypt(data) data = inst.pass_decrypt(data)
@ -167,7 +167,7 @@ class Settings(dict, Singleton):
def save(self): def save(self):
text = json.dumps(self) text = json.dumps(self)
inst = ToxEncryptSave.get_instance() inst = ToxES.get_instance()
if inst.has_password(): if inst.has_password():
text = bytes(inst.pass_encrypt(bytes(text, 'utf-8'))) text = bytes(inst.pass_encrypt(bytes(text, 'utf-8')))
else: else:
@ -252,7 +252,7 @@ class ProfileHelper(Singleton):
return self._directory return self._directory
def save_profile(self, data): def save_profile(self, data):
inst = ToxEncryptSave.get_instance() inst = ToxES.get_instance()
if inst.has_password(): if inst.has_password():
data = inst.pass_encrypt(data) data = inst.pass_encrypt(data)
with open(self._path, 'wb') as fl: with open(self._path, 'wb') as fl:

View file

@ -1,56 +1,13 @@
import libtox import libtox
import util
from ctypes import c_size_t, create_string_buffer, byref, c_int, ArgumentError, c_char_p, c_bool from ctypes import c_size_t, create_string_buffer, byref, c_int, ArgumentError, c_char_p, c_bool
from toxencryptsave_enums_and_consts import *
TOX_ERR_ENCRYPTION = { class ToxEncryptSave:
# The function returned successfully.
'OK': 0,
# Some input data, or maybe the output pointer, was null.
'NULL': 1,
# The crypto lib was unable to derive a key from the given passphrase, which is usually a lack of memory issue. The
# functions accepting keys do not produce this error.
'KEY_DERIVATION_FAILED': 2,
# The encryption itself failed.
'FAILED': 3
}
TOX_ERR_DECRYPTION = {
# The function returned successfully.
'OK': 0,
# Some input data, or maybe the output pointer, was null.
'NULL': 1,
# The input data was shorter than TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes
'INVALID_LENGTH': 2,
# The input data is missing the magic number (i.e. wasn't created by this module, or is corrupted)
'BAD_FORMAT': 3,
# The crypto lib was unable to derive a key from the given passphrase, which is usually a lack of memory issue. The
# functions accepting keys do not produce this error.
'KEY_DERIVATION_FAILED': 4,
# The encrypted byte array could not be decrypted. Either the data was corrupt or the password/key was incorrect.
'FAILED': 5,
}
TOX_PASS_ENCRYPTION_EXTRA_LENGTH = 80
# TODO: move logic to separate class
class ToxEncryptSave(util.Singleton):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.libtoxencryptsave = libtox.LibToxEncryptSave() self.libtoxencryptsave = libtox.LibToxEncryptSave()
self._passphrase = None
def set_password(self, passphrase):
self._passphrase = passphrase
def has_password(self):
return bool(self._passphrase)
def is_password(self, password):
return self._passphrase == password
def is_data_encrypted(self, data): def is_data_encrypted(self, data):
func = self.libtoxencryptsave.tox_is_data_encrypted func = self.libtoxencryptsave.tox_is_data_encrypted
@ -58,9 +15,9 @@ class ToxEncryptSave(util.Singleton):
result = func(c_char_p(bytes(data))) result = func(c_char_p(bytes(data)))
return result return result
def pass_encrypt(self, data): def pass_encrypt(self, data, password):
""" """
Encrypts the given data with the given passphrase. Encrypts the given data with the given password.
:return: output array :return: output array
""" """
@ -68,8 +25,8 @@ class ToxEncryptSave(util.Singleton):
tox_err_encryption = c_int() tox_err_encryption = c_int()
self.libtoxencryptsave.tox_pass_encrypt(c_char_p(data), self.libtoxencryptsave.tox_pass_encrypt(c_char_p(data),
c_size_t(len(data)), c_size_t(len(data)),
c_char_p(bytes(self._passphrase, 'utf-8')), c_char_p(bytes(password, 'utf-8')),
c_size_t(len(self._passphrase)), c_size_t(len(password)),
out, out,
byref(tox_err_encryption)) byref(tox_err_encryption))
tox_err_encryption = tox_err_encryption.value tox_err_encryption = tox_err_encryption.value
@ -83,9 +40,9 @@ class ToxEncryptSave(util.Singleton):
elif tox_err_encryption == TOX_ERR_ENCRYPTION['FAILED']: elif tox_err_encryption == TOX_ERR_ENCRYPTION['FAILED']:
raise RuntimeError('The encryption itself failed.') raise RuntimeError('The encryption itself failed.')
def pass_decrypt(self, data): def pass_decrypt(self, data, password):
""" """
Decrypts the given data with the given passphrase. Decrypts the given data with the given password.
:return: output array :return: output array
""" """
@ -93,8 +50,8 @@ class ToxEncryptSave(util.Singleton):
tox_err_decryption = c_int() tox_err_decryption = c_int()
self.libtoxencryptsave.tox_pass_decrypt(c_char_p(bytes(data)), self.libtoxencryptsave.tox_pass_decrypt(c_char_p(bytes(data)),
c_size_t(len(data)), c_size_t(len(data)),
c_char_p(bytes(self._passphrase, 'utf-8')), c_char_p(bytes(password, 'utf-8')),
c_size_t(len(self._passphrase)), c_size_t(len(password)),
out, out,
byref(tox_err_decryption)) byref(tox_err_decryption))
tox_err_decryption = tox_err_decryption.value tox_err_decryption = tox_err_decryption.value

View file

@ -0,0 +1,29 @@
TOX_ERR_ENCRYPTION = {
# The function returned successfully.
'OK': 0,
# Some input data, or maybe the output pointer, was null.
'NULL': 1,
# The crypto lib was unable to derive a key from the given passphrase, which is usually a lack of memory issue. The
# functions accepting keys do not produce this error.
'KEY_DERIVATION_FAILED': 2,
# The encryption itself failed.
'FAILED': 3
}
TOX_ERR_DECRYPTION = {
# The function returned successfully.
'OK': 0,
# Some input data, or maybe the output pointer, was null.
'NULL': 1,
# The input data was shorter than TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes
'INVALID_LENGTH': 2,
# The input data is missing the magic number (i.e. wasn't created by this module, or is corrupted)
'BAD_FORMAT': 3,
# The crypto lib was unable to derive a key from the given passphrase, which is usually a lack of memory issue. The
# functions accepting keys do not produce this error.
'KEY_DERIVATION_FAILED': 4,
# The encrypted byte array could not be decrypted. Either the data was corrupt or the password/key was incorrect.
'FAILED': 5,
}
TOX_PASS_ENCRYPTION_EXTRA_LENGTH = 80

28
toxygen/toxes.py Normal file
View file

@ -0,0 +1,28 @@
import util
import toxencryptsave
class ToxES(util.Singleton):
def __init__(self):
super().__init__()
self._toxencryptsave = toxencryptsave.ToxEncryptSave()
self._passphrase = None
def set_password(self, passphrase):
self._passphrase = passphrase
def has_password(self):
return bool(self._passphrase)
def is_password(self, password):
return self._passphrase == password
def is_data_encrypted(self, data):
return len(data) > 0 and self._toxencryptsave.is_data_encrypted(data)
def pass_encrypt(self, data):
return self._toxencryptsave.pass_encrypt(data, self._passphrase)
def pass_decrypt(self, data):
return self._toxencryptsave.pass_decrypt(data, self._passphrase)