[omemo] Make encrypt_file() method better available
Now we dont need to supply the account
This commit is contained in:
@@ -45,6 +45,7 @@ try:
|
|||||||
from cryptography.hazmat.primitives.ciphers import algorithms
|
from cryptography.hazmat.primitives.ciphers import algorithms
|
||||||
from cryptography.hazmat.primitives.ciphers.modes import GCM
|
from cryptography.hazmat.primitives.ciphers.modes import GCM
|
||||||
from cryptography.hazmat.backends import default_backend
|
from cryptography.hazmat.backends import default_backend
|
||||||
|
from omemo.omemo.aes_gcm_native import aes_encrypt
|
||||||
except ImportError:
|
except ImportError:
|
||||||
log.exception('ImportError')
|
log.exception('ImportError')
|
||||||
ERROR = True
|
ERROR = True
|
||||||
@@ -59,6 +60,15 @@ except Exception:
|
|||||||
log.exception('Error')
|
log.exception('Error')
|
||||||
|
|
||||||
|
|
||||||
|
def encrypt_file(data):
|
||||||
|
key = os.urandom(32)
|
||||||
|
iv = os.urandom(16)
|
||||||
|
|
||||||
|
payload, tag = aes_encrypt(key, iv, data)
|
||||||
|
encrypted_data = payload + tag
|
||||||
|
return (encrypted_data, key, iv)
|
||||||
|
|
||||||
|
|
||||||
class File:
|
class File:
|
||||||
def __init__(self, url):
|
def __init__(self, url):
|
||||||
self.url, self.fragment = urldefrag(url)
|
self.url, self.fragment = urldefrag(url)
|
||||||
@@ -217,14 +217,6 @@ class OmemoState:
|
|||||||
log.debug("Decrypted Message => " + result)
|
log.debug("Decrypted Message => " + result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def encrypt_file(self, data):
|
|
||||||
key = os.urandom(32)
|
|
||||||
iv = os.urandom(16)
|
|
||||||
|
|
||||||
payload, tag = encrypt(key, iv, data)
|
|
||||||
encrypted_data = payload + tag
|
|
||||||
return (encrypted_data, key, iv)
|
|
||||||
|
|
||||||
def create_msg(self, from_jid, jid, plaintext):
|
def create_msg(self, from_jid, jid, plaintext):
|
||||||
key = get_random_bytes(16)
|
key = get_random_bytes(16)
|
||||||
iv = get_random_bytes(16)
|
iv = get_random_bytes(16)
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ ERROR_MSG = ''
|
|||||||
log = logging.getLogger('gajim.plugin_system.omemo')
|
log = logging.getLogger('gajim.plugin_system.omemo')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from omemo.file_decryption import FileDecryption
|
from omemo import file_crypto
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
log.exception(error)
|
log.exception(error)
|
||||||
ERROR_MSG = CRYPTOGRAPHY_MISSING
|
ERROR_MSG = CRYPTOGRAPHY_MISSING
|
||||||
@@ -188,17 +188,19 @@ class OmemoPlugin(GajimPlugin):
|
|||||||
self.connections[conn.name].encrypt_message(conn, obj, callback)
|
self.connections[conn.name].encrypt_message(conn, obj, callback)
|
||||||
|
|
||||||
def _file_decryption(self, url, kind, instance, window):
|
def _file_decryption(self, url, kind, instance, window):
|
||||||
FileDecryption(self).hyperlink_handler(url, kind, instance, window)
|
file_decryption.FileDecryption(self).hyperlink_handler(
|
||||||
|
url, kind, instance, window)
|
||||||
|
|
||||||
def encrypt_file(self, file, account, callback):
|
def encrypt_file(self, file, callback):
|
||||||
thread = threading.Thread(target=self._encrypt_file_thread,
|
thread = threading.Thread(target=self._encrypt_file_thread,
|
||||||
args=(file, account, callback))
|
args=(file, callback))
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
def _encrypt_file_thread(self, file, account, callback):
|
@staticmethod
|
||||||
omemo = self.get_omemo(account)
|
def _encrypt_file_thread(file, callback):
|
||||||
encrypted_data, key, iv = omemo.encrypt_file(file.get_data(full=True))
|
encrypted_data, key, iv = file_decryption.encrypt_file(
|
||||||
|
file.get_data(full=True))
|
||||||
file.encrypted = True
|
file.encrypted = True
|
||||||
file.size = len(encrypted_data)
|
file.size = len(encrypted_data)
|
||||||
file.user_data = binascii.hexlify(iv + key).decode('utf-8')
|
file.user_data = binascii.hexlify(iv + key).decode('utf-8')
|
||||||
|
|||||||
Reference in New Issue
Block a user