toxygen_wrapper/wrapper/toxencryptsave.py

94 lines
4.6 KiB
Python
Raw Normal View History

2022-09-24 04:00:32 +00:00
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
2022-09-25 07:21:16 +00:00
try:
from wrapper import libtox
2023-12-11 15:06:07 +00:00
#? from wrapper.toxencryptsave_enums_and_consts import *
import wrapper.toxencryptsave_enums_and_consts as enum
2022-09-25 07:21:16 +00:00
except:
import libtox
2023-12-11 15:06:07 +00:00
#? from toxencryptsave_enums_and_consts import *
import toxencryptsave_enums_and_consts as enum
2022-09-24 04:00:32 +00:00
2023-12-15 02:26:51 +00:00
from typing import Union, Callable
2022-11-17 14:18:51 +00:00
from ctypes import (ArgumentError, byref, c_bool, c_char_p, c_int, c_size_t,
2023-12-15 02:26:51 +00:00
create_string_buffer, Array)
2022-11-17 14:18:51 +00:00
2022-10-29 18:44:01 +00:00
2022-09-24 04:00:32 +00:00
class ToxEncryptSave:
def __init__(self):
self.libtoxencryptsave = libtox.LibToxEncryptSave()
2023-12-11 14:37:04 +00:00
def is_data_encrypted(self, data: bytes) -> bool:
2022-09-24 04:00:32 +00:00
"""
Checks if given data is encrypted
"""
func = self.libtoxencryptsave.tox_is_data_encrypted
func.restype = c_bool
result = func(c_char_p(bytes(data)))
2023-12-11 14:37:04 +00:00
return bool(result)
2022-09-24 04:00:32 +00:00
2023-12-14 20:46:56 +00:00
def pass_encrypt(self, data: bytes, password: Union[str,bytes]) -> bytes:
2022-09-24 04:00:32 +00:00
"""
Encrypts the given data with the given password.
:return: output array
"""
2023-12-11 15:06:07 +00:00
out = create_string_buffer(len(data) + enum.TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
2022-09-24 04:00:32 +00:00
tox_err_encryption = c_int()
2023-12-14 20:46:56 +00:00
assert password
2023-12-11 14:37:04 +00:00
if type(password) != bytes:
password = bytes(password, 'utf-8')
2022-09-24 04:00:32 +00:00
self.libtoxencryptsave.tox_pass_encrypt(c_char_p(data),
c_size_t(len(data)),
2023-12-11 14:37:04 +00:00
c_char_p(password),
2022-09-24 04:00:32 +00:00
c_size_t(len(password)),
out,
byref(tox_err_encryption))
tox_err_encryption = tox_err_encryption.value
2023-12-11 15:06:07 +00:00
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['OK']:
2023-12-14 20:46:56 +00:00
return bytes(out[:])
2023-12-13 00:57:28 +00:00
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['NULL']:
2022-09-24 04:00:32 +00:00
raise ArgumentError('Some input data, or maybe the output pointer, was null.')
2023-12-13 00:57:28 +00:00
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['KEY_DERIVATION_FAILED']:
2022-09-24 04:00:32 +00:00
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.')
2023-12-13 00:57:28 +00:00
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['FAILED']:
2022-09-24 04:00:32 +00:00
raise RuntimeError('The encryption itself failed.')
2023-12-13 00:57:28 +00:00
raise ToxError('The function did not return OK.')
2022-09-24 04:00:32 +00:00
2023-12-14 20:46:56 +00:00
def pass_decrypt(self, data: bytes, password: Union[str,bytes]) -> bytes:
2022-09-24 04:00:32 +00:00
"""
Decrypts the given data with the given password.
:return: output array
"""
2023-12-12 05:20:05 +00:00
out = create_string_buffer(len(data) - enum.TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
2022-09-24 04:00:32 +00:00
tox_err_decryption = c_int()
2023-12-14 20:46:56 +00:00
assert password
2023-12-11 15:06:07 +00:00
if type(password) != bytes:
password = bytes(password, 'utf-8')
2022-09-24 04:00:32 +00:00
self.libtoxencryptsave.tox_pass_decrypt(c_char_p(bytes(data)),
c_size_t(len(data)),
2023-12-11 15:06:07 +00:00
c_char_p(password),
2022-09-24 04:00:32 +00:00
c_size_t(len(password)),
out,
byref(tox_err_decryption))
tox_err_decryption = tox_err_decryption.value
2023-12-11 15:06:07 +00:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['OK']:
2023-12-11 14:37:04 +00:00
return bytes(out[:])
2023-12-14 20:46:56 +00:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['NULL']:
2022-09-24 04:00:32 +00:00
raise ArgumentError('Some input data, or maybe the output pointer, was null.')
2023-12-14 20:46:56 +00:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['INVALID_LENGTH']:
2022-09-24 04:00:32 +00:00
raise ArgumentError('The input data was shorter than TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes')
2023-12-14 20:46:56 +00:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['BAD_FORMAT']:
2022-09-24 04:00:32 +00:00
raise ArgumentError('The input data is missing the magic number (i.e. wasn\'t created by this module, or is'
' corrupted)')
2023-12-14 20:46:56 +00:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['KEY_DERIVATION_FAILED']:
2022-09-24 04:00:32 +00:00
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.')
2023-12-14 20:46:56 +00:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['FAILED']:
2022-09-24 04:00:32 +00:00
raise RuntimeError('The encrypted byte array could not be decrypted. Either the data was corrupt or the '
'password/key was incorrect.')
2023-12-14 20:46:56 +00:00
raise ToxError('The function did not return OK.')