This commit is contained in:
emdee@macaw.me
2023-12-14 20:46:56 +00:00
parent b934928fe3
commit 012c7ea56e
8 changed files with 388 additions and 286 deletions

View File

@@ -27,7 +27,7 @@ class ToxEncryptSave:
result = func(c_char_p(bytes(data)))
return bool(result)
def pass_encrypt(self, data: bytes, password: str) -> bytes:
def pass_encrypt(self, data: bytes, password: Union[str,bytes]) -> bytes:
"""
Encrypts the given data with the given password.
@@ -35,6 +35,7 @@ class ToxEncryptSave:
"""
out = create_string_buffer(len(data) + enum.TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
tox_err_encryption = c_int()
assert password
if type(password) != bytes:
password = bytes(password, 'utf-8')
self.libtoxencryptsave.tox_pass_encrypt(c_char_p(data),
@@ -45,7 +46,7 @@ class ToxEncryptSave:
byref(tox_err_encryption))
tox_err_encryption = tox_err_encryption.value
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['OK']:
return out[:]
return bytes(out[:])
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['NULL']:
raise ArgumentError('Some input data, or maybe the output pointer, was null.')
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['KEY_DERIVATION_FAILED']:
@@ -55,7 +56,7 @@ class ToxEncryptSave:
raise RuntimeError('The encryption itself failed.')
raise ToxError('The function did not return OK.')
def pass_decrypt(self, data: bytes, password: str) -> bytes:
def pass_decrypt(self, data: bytes, password: Union[str,bytes]) -> bytes:
"""
Decrypts the given data with the given password.
@@ -63,6 +64,7 @@ class ToxEncryptSave:
"""
out = create_string_buffer(len(data) - enum.TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
tox_err_decryption = c_int()
assert password
if type(password) != bytes:
password = bytes(password, 'utf-8')
self.libtoxencryptsave.tox_pass_decrypt(c_char_p(bytes(data)),
@@ -74,16 +76,17 @@ class ToxEncryptSave:
tox_err_decryption = tox_err_decryption.value
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['OK']:
return bytes(out[:])
elif tox_err_decryption == enum.TOX_ERR_DECRYPTION['NULL']:
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['NULL']:
raise ArgumentError('Some input data, or maybe the output pointer, was null.')
elif tox_err_decryption == enum.TOX_ERR_DECRYPTION['INVALID_LENGTH']:
if 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 == enum.TOX_ERR_DECRYPTION['BAD_FORMAT']:
if 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 == enum.TOX_ERR_DECRYPTION['KEY_DERIVATION_FAILED']:
if 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 == enum.TOX_ERR_DECRYPTION['FAILED']:
if 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.')
raise ToxError('The function did not return OK.')