2016-05-14 11:29:54 +02:00
|
|
|
import libtox
|
2016-05-15 16:39:49 +02:00
|
|
|
from ctypes import c_size_t, create_string_buffer, byref, c_int, ArgumentError, c_char_p, c_bool
|
2017-02-11 18:07:28 +01:00
|
|
|
from toxencryptsave_enums_and_consts import *
|
2016-05-14 11:29:54 +02:00
|
|
|
|
|
|
|
|
2017-02-11 18:07:28 +01:00
|
|
|
class ToxEncryptSave:
|
2016-05-14 11:29:54 +02:00
|
|
|
|
2016-05-15 15:45:05 +02:00
|
|
|
def __init__(self):
|
2016-07-28 22:21:57 +02:00
|
|
|
self.libtoxencryptsave = libtox.LibToxEncryptSave()
|
2016-07-02 14:40:06 +02:00
|
|
|
|
2016-05-15 15:45:05 +02:00
|
|
|
def is_data_encrypted(self, data):
|
2017-02-11 20:04:32 +01:00
|
|
|
"""
|
|
|
|
Checks if given data is encrypted
|
|
|
|
"""
|
2016-05-15 16:39:49 +02:00
|
|
|
func = self.libtoxencryptsave.tox_is_data_encrypted
|
|
|
|
func.restype = c_bool
|
2016-06-21 13:58:11 +02:00
|
|
|
result = func(c_char_p(bytes(data)))
|
2016-05-15 16:39:49 +02:00
|
|
|
return result
|
2016-05-15 15:45:05 +02:00
|
|
|
|
2017-02-11 18:07:28 +01:00
|
|
|
def pass_encrypt(self, data, password):
|
2016-05-15 10:00:45 +02:00
|
|
|
"""
|
2017-02-11 18:07:28 +01:00
|
|
|
Encrypts the given data with the given password.
|
2016-05-15 10:00:45 +02:00
|
|
|
|
|
|
|
:return: output array
|
|
|
|
"""
|
|
|
|
out = create_string_buffer(len(data) + TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
|
|
|
|
tox_err_encryption = c_int()
|
2016-06-21 22:43:43 +02:00
|
|
|
self.libtoxencryptsave.tox_pass_encrypt(c_char_p(data),
|
2016-05-15 12:39:03 +02:00
|
|
|
c_size_t(len(data)),
|
2017-02-11 18:07:28 +01:00
|
|
|
c_char_p(bytes(password, 'utf-8')),
|
|
|
|
c_size_t(len(password)),
|
2016-05-15 12:39:03 +02:00
|
|
|
out,
|
|
|
|
byref(tox_err_encryption))
|
2016-05-15 10:00:45 +02:00
|
|
|
tox_err_encryption = tox_err_encryption.value
|
|
|
|
if tox_err_encryption == TOX_ERR_ENCRYPTION['OK']:
|
2016-05-15 12:39:03 +02:00
|
|
|
return out[:]
|
2016-05-15 10:00:45 +02:00
|
|
|
elif tox_err_encryption == TOX_ERR_ENCRYPTION['NULL']:
|
|
|
|
raise ArgumentError('Some input data, or maybe the output pointer, was null.')
|
|
|
|
elif tox_err_encryption == TOX_ERR_ENCRYPTION['KEY_DERIVATION_FAILED']:
|
|
|
|
raise RuntimeError('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.')
|
|
|
|
elif tox_err_encryption == TOX_ERR_ENCRYPTION['FAILED']:
|
|
|
|
raise RuntimeError('The encryption itself failed.')
|
|
|
|
|
2017-02-11 18:07:28 +01:00
|
|
|
def pass_decrypt(self, data, password):
|
2016-05-15 10:00:45 +02:00
|
|
|
"""
|
2017-02-11 18:07:28 +01:00
|
|
|
Decrypts the given data with the given password.
|
2016-05-14 11:29:54 +02:00
|
|
|
|
2016-05-15 10:00:45 +02:00
|
|
|
:return: output array
|
|
|
|
"""
|
|
|
|
out = create_string_buffer(len(data) - TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
|
|
|
|
tox_err_decryption = c_int()
|
2016-06-21 13:58:11 +02:00
|
|
|
self.libtoxencryptsave.tox_pass_decrypt(c_char_p(bytes(data)),
|
2016-05-15 12:39:03 +02:00
|
|
|
c_size_t(len(data)),
|
2017-02-11 18:07:28 +01:00
|
|
|
c_char_p(bytes(password, 'utf-8')),
|
|
|
|
c_size_t(len(password)),
|
2016-05-15 12:39:03 +02:00
|
|
|
out,
|
|
|
|
byref(tox_err_decryption))
|
2016-05-15 10:00:45 +02:00
|
|
|
tox_err_decryption = tox_err_decryption.value
|
|
|
|
if tox_err_decryption == TOX_ERR_DECRYPTION['OK']:
|
2016-05-15 12:39:03 +02:00
|
|
|
return out[:]
|
2016-05-15 10:00:45 +02:00
|
|
|
elif tox_err_decryption == TOX_ERR_DECRYPTION['NULL']:
|
|
|
|
raise ArgumentError('Some input data, or maybe the output pointer, was null.')
|
|
|
|
elif tox_err_decryption == TOX_ERR_DECRYPTION['INVALID_LENGTH']:
|
|
|
|
raise ArgumentError('The input data was shorter than TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes')
|
|
|
|
elif tox_err_decryption == TOX_ERR_DECRYPTION['BAD_FORMAT']:
|
|
|
|
raise ArgumentError('The input data is missing the magic number (i.e. wasn\'t created by this module, or is'
|
|
|
|
' corrupted)')
|
|
|
|
elif tox_err_decryption == TOX_ERR_DECRYPTION['KEY_DERIVATION_FAILED']:
|
|
|
|
raise RuntimeError('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.')
|
|
|
|
elif tox_err_decryption == TOX_ERR_DECRYPTION['FAILED']:
|
|
|
|
raise RuntimeError('The encrypted byte array could not be decrypted. Either the data was corrupt or the '
|
|
|
|
'password/key was incorrect.')
|