typing
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
|
||||
try:
|
||||
from wrapper import libtox
|
||||
from wrapper.toxencryptsave_enums_and_consts import *
|
||||
#? from wrapper.toxencryptsave_enums_and_consts import *
|
||||
import wrapper.toxencryptsave_enums_and_consts as enum
|
||||
except:
|
||||
import libtox
|
||||
from toxencryptsave_enums_and_consts import *
|
||||
#? from toxencryptsave_enums_and_consts import *
|
||||
import toxencryptsave_enums_and_consts as enum
|
||||
|
||||
from ctypes import (ArgumentError, byref, c_bool, c_char_p, c_int, c_size_t,
|
||||
create_string_buffer)
|
||||
@@ -31,7 +33,7 @@ class ToxEncryptSave:
|
||||
|
||||
:return: output array
|
||||
"""
|
||||
out = create_string_buffer(len(data) + TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
|
||||
out = create_string_buffer(len(data) + enum.TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
|
||||
tox_err_encryption = c_int()
|
||||
if type(password) != bytes:
|
||||
password = bytes(password, 'utf-8')
|
||||
@@ -42,17 +44,17 @@ class ToxEncryptSave:
|
||||
out,
|
||||
byref(tox_err_encryption))
|
||||
tox_err_encryption = tox_err_encryption.value
|
||||
if tox_err_encryption == TOX_ERR_ENCRYPTION['OK']:
|
||||
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['OK']:
|
||||
return out[:]
|
||||
elif tox_err_encryption == TOX_ERR_ENCRYPTION['NULL']:
|
||||
elif tox_err_encryption == enum.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']:
|
||||
elif tox_err_encryption == enum.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']:
|
||||
elif tox_err_encryption == enum.TOX_ERR_ENCRYPTION['FAILED']:
|
||||
raise RuntimeError('The encryption itself failed.')
|
||||
|
||||
def pass_decrypt(self, data, password) -> bytes:
|
||||
def pass_decrypt(self, data: bytes, password: str) -> bytes:
|
||||
"""
|
||||
Decrypts the given data with the given password.
|
||||
|
||||
@@ -60,25 +62,27 @@ class ToxEncryptSave:
|
||||
"""
|
||||
out = create_string_buffer(len(data) - TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
|
||||
tox_err_decryption = c_int()
|
||||
if type(password) != bytes:
|
||||
password = bytes(password, 'utf-8')
|
||||
self.libtoxencryptsave.tox_pass_decrypt(c_char_p(bytes(data)),
|
||||
c_size_t(len(data)),
|
||||
c_char_p(bytes(password, 'utf-8')),
|
||||
c_char_p(password),
|
||||
c_size_t(len(password)),
|
||||
out,
|
||||
byref(tox_err_decryption))
|
||||
tox_err_decryption = tox_err_decryption.value
|
||||
if tox_err_decryption == TOX_ERR_DECRYPTION['OK']:
|
||||
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['OK']:
|
||||
return bytes(out[:])
|
||||
elif tox_err_decryption == TOX_ERR_DECRYPTION['NULL']:
|
||||
elif tox_err_decryption == enum.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']:
|
||||
elif tox_err_decryption == enum.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']:
|
||||
elif tox_err_decryption == enum.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']:
|
||||
elif tox_err_decryption == enum.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']:
|
||||
elif tox_err_decryption == enum.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.')
|
||||
|
||||
Reference in New Issue
Block a user