actually remove dependency on future

master
Hubert Chathi 2023-04-26 10:00:34 -04:00
parent 418656ee9f
commit afb3d403e1
8 changed files with 42 additions and 65 deletions

View File

@ -32,8 +32,6 @@ import json
from builtins import bytes, super
from typing import AnyStr, Dict, Optional, Type
from future.utils import bytes_to_native_str
# pylint: disable=no-name-in-module
from _libolm import ffi, lib # type: ignore
@ -93,8 +91,7 @@ class Account(object):
if ret != lib.olm_error():
return
last_error = bytes_to_native_str(
ffi.string((lib.olm_account_last_error(self._account))))
last_error = ffi.string((lib.olm_account_last_error(self._account))).decode()
raise OlmAccountError(last_error)
@ -209,7 +206,7 @@ class Account(object):
for i in range(0, len(bytes_message)):
bytes_message[i] = 0
return bytes_to_native_str(ffi.unpack(out_buffer, out_length))
return ffi.unpack(out_buffer, out_length).decode()
@property
def max_one_time_keys(self):

View File

@ -28,8 +28,6 @@ Examples:
from builtins import bytes, super
from typing import AnyStr, Optional, Tuple, Type
from future.utils import bytes_to_native_str
# pylint: disable=no-name-in-module
from _libolm import ffi, lib # type: ignore
@ -171,8 +169,9 @@ class InboundGroupSession(object):
if ret != lib.olm_error():
return
last_error = bytes_to_native_str(ffi.string(
lib.olm_inbound_group_session_last_error(self._session)))
last_error = ffi.string(
lib.olm_inbound_group_session_last_error(self._session)
).decode()
raise OlmGroupSessionError(last_error)
@ -252,7 +251,7 @@ class InboundGroupSession(object):
id_length
)
self._check_error(ret)
return bytes_to_native_str(ffi.unpack(id_buffer, id_length))
return ffi.unpack(id_buffer, id_length).decode()
@property
def first_known_index(self):
@ -290,7 +289,7 @@ class InboundGroupSession(object):
message_index
)
self._check_error(ret)
export_str = bytes_to_native_str(ffi.unpack(export_buffer, export_length))
export_str = ffi.unpack(export_buffer, export_length).decode()
# clear out copies of the key
lib.memset(export_buffer, 0, export_length)
@ -373,9 +372,9 @@ class OutboundGroupSession(object):
if ret != lib.olm_error():
return
last_error = bytes_to_native_str(ffi.string(
last_error = ffi.string(
lib.olm_outbound_group_session_last_error(self._session)
))
).decode()
raise OlmGroupSessionError(last_error)
@ -483,7 +482,7 @@ class OutboundGroupSession(object):
for i in range(0, len(byte_plaintext)):
byte_plaintext[i] = 0
return bytes_to_native_str(ffi.unpack(message_buffer, message_length))
return ffi.unpack(message_buffer, message_length).decode()
@property
def id(self):
@ -499,7 +498,7 @@ class OutboundGroupSession(object):
)
self._check_error(ret)
return bytes_to_native_str(ffi.unpack(id_buffer, id_length))
return ffi.unpack(id_buffer, id_length).decode()
@property
def message_index(self):
@ -529,4 +528,4 @@ class OutboundGroupSession(object):
)
self._check_error(ret)
return bytes_to_native_str(ffi.unpack(key_buffer, key_length))
return ffi.unpack(key_buffer, key_length).decode()

View File

@ -36,8 +36,6 @@ Examples:
from builtins import super
from typing import AnyStr, Type
from future.utils import bytes_to_native_str
from _libolm import ffi, lib # type: ignore
from ._compat import URANDOM, to_bytearray, to_unicode_str
@ -116,8 +114,9 @@ class PkEncryption(object):
if ret != lib.olm_error():
return
last_error = bytes_to_native_str(
ffi.string(lib.olm_pk_encryption_last_error(self._pk_encryption)))
last_error = ffi.string(
lib.olm_pk_encryption_last_error(self._pk_encryption)
).decode()
raise PkEncryptionError(last_error)
@ -166,12 +165,9 @@ class PkEncryption(object):
byte_plaintext[i] = 0
message = PkMessage(
bytes_to_native_str(
ffi.unpack(ephemeral_key, ephemeral_key_size)),
bytes_to_native_str(
ffi.unpack(mac, mac_length)),
bytes_to_native_str(
ffi.unpack(ciphertext, ciphertext_length))
ffi.unpack(ephemeral_key, ephemeral_key_size).decode(),
ffi.unpack(mac, mac_length).decode(),
ffi.unpack(ciphertext, ciphertext_length).decode(),
)
return message
@ -217,18 +213,19 @@ class PkDecryption(object):
random_buffer, random_length
)
self._check_error(ret)
self.public_key: str = bytes_to_native_str(ffi.unpack(
self.public_key: str = ffi.unpack(
key_buffer,
key_length
))
).decode()
def _check_error(self, ret):
# type: (int) -> None
if ret != lib.olm_error():
return
last_error = bytes_to_native_str(
ffi.string(lib.olm_pk_decryption_last_error(self._pk_decryption)))
last_error = ffi.string(
lib.olm_pk_decryption_last_error(self._pk_decryption)
).decode()
raise PkDecryptionError(last_error)
@ -306,10 +303,10 @@ class PkDecryption(object):
for i in range(0, len(byte_key)):
byte_key[i] = 0
obj.public_key = bytes_to_native_str(ffi.unpack(
obj.public_key = ffi.unpack(
pubkey_buffer,
pubkey_length
))
).decode()
return obj
@ -411,17 +408,14 @@ class PkSigning(object):
self._check_error(ret)
self.public_key = bytes_to_native_str(
ffi.unpack(pubkey_buffer, pubkey_length)
)
self.public_key = ffi.unpack(pubkey_buffer, pubkey_length).decode()
def _check_error(self, ret):
# type: (int) -> None
if ret != lib.olm_error():
return
last_error = bytes_to_native_str(
ffi.string(lib.olm_pk_signing_last_error(self._pk_signing)))
last_error = ffi.string(lib.olm_pk_signing_last_error(self._pk_signing)).decode()
raise PkSigningError(last_error)
@ -456,6 +450,4 @@ class PkSigning(object):
signature_buffer, signature_length)
self._check_error(ret)
return bytes_to_native_str(
ffi.unpack(signature_buffer, signature_length)
)
return ffi.unpack(signature_buffer, signature_length).decode()

View File

@ -34,8 +34,6 @@ from builtins import bytes
from functools import wraps
from typing import Optional
from future.utils import bytes_to_native_str
from _libolm import ffi, lib
from ._compat import URANDOM, to_bytearray, to_bytes
@ -92,8 +90,7 @@ class Sas(object):
if ret != lib.olm_error():
return
last_error = bytes_to_native_str(
ffi.string((lib.olm_sas_last_error(self._sas))))
last_error = ffi.string((lib.olm_sas_last_error(self._sas))).decode()
raise OlmSasError(last_error)
@ -115,7 +112,7 @@ class Sas(object):
lib.olm_sas_get_pubkey(self._sas, pubkey_buffer, pubkey_length)
)
return bytes_to_native_str(ffi.unpack(pubkey_buffer, pubkey_length))
return ffi.unpack(pubkey_buffer, pubkey_length).decode()
@property
def other_key_set(self):
@ -208,7 +205,7 @@ class Sas(object):
mac_length
)
)
return bytes_to_native_str(ffi.unpack(mac_buffer, mac_length))
return ffi.unpack(mac_buffer, mac_length).decode()
def calculate_mac_fixed_base64(self, message, extra_info):
# type: (str, str) -> str
@ -242,7 +239,7 @@ class Sas(object):
mac_length
)
)
return bytes_to_native_str(ffi.unpack(mac_buffer, mac_length))
return ffi.unpack(mac_buffer, mac_length).decode()
def calculate_mac_long_kdf(self, message, extra_info):
# type: (str, str) -> str
@ -276,4 +273,4 @@ class Sas(object):
mac_length
)
)
return bytes_to_native_str(ffi.unpack(mac_buffer, mac_length))
return ffi.unpack(mac_buffer, mac_length).decode()

View File

@ -35,8 +35,6 @@ Examples:
from builtins import bytes, super
from typing import AnyStr, Optional, Type
from future.utils import bytes_to_native_str
# pylint: disable=no-name-in-module
from _libolm import ffi, lib # type: ignore
@ -146,8 +144,7 @@ class Session(object):
if ret != lib.olm_error():
return
last_error = bytes_to_native_str(
ffi.string(lib.olm_session_last_error(self._session)))
last_error = ffi.string(lib.olm_session_last_error(self._session)).decode()
raise OlmSessionError(last_error)
@ -260,16 +257,16 @@ class Session(object):
if message_type == lib.OLM_MESSAGE_TYPE_PRE_KEY:
return OlmPreKeyMessage(
bytes_to_native_str(ffi.unpack(
ffi.unpack(
ciphertext_buffer,
ciphertext_length
)))
).decode())
elif message_type == lib.OLM_MESSAGE_TYPE_MESSAGE:
return OlmMessage(
bytes_to_native_str(ffi.unpack(
ffi.unpack(
ciphertext_buffer,
ciphertext_length
)))
).decode())
else: # pragma: no cover
raise ValueError("Unknown message type")
@ -340,7 +337,7 @@ class Session(object):
self._check_error(
lib.olm_session_id(self._session, id_buffer, id_length)
)
return bytes_to_native_str(ffi.unpack(id_buffer, id_length))
return ffi.unpack(id_buffer, id_length).decode()
def matches(self, message, identity_key=None):
# type: (OlmPreKeyMessage, Optional[AnyStr]) -> bool
@ -407,7 +404,7 @@ class Session(object):
lib.olm_session_describe(
self._session, describe_buffer, buffer_length
)
return bytes_to_native_str(ffi.string(describe_buffer))
return ffi.string(describe_buffer).decode()
class InboundSession(Session):

View File

@ -33,8 +33,6 @@ Examples:
# pylint: disable=redefined-builtin,unused-import
from typing import AnyStr, Type
from future.utils import bytes_to_native_str
# pylint: disable=no-name-in-module
from _libolm import ffi, lib # type: ignore
@ -123,7 +121,7 @@ class _Utility(object):
cls._check_error(ret, OlmHashError)
return bytes_to_native_str(ffi.unpack(hash, hash_length))
return ffi.unpack(hash, hash_length).decode()
def ed25519_verify(key, message, signature):

View File

@ -24,7 +24,6 @@ setup(
cffi_modules=["olm_build.py:ffibuilder"],
install_requires=[
"cffi>=1.0.0",
"future",
"typing;python_version<'3.5'"
],
zip_safe=False,

View File

@ -1,8 +1,6 @@
import base64
import hashlib
from future.utils import bytes_to_native_str
from olm import sha256
from olm._compat import to_bytes
@ -19,7 +17,7 @@ class TestClass(object):
hashlib.sha256(to_bytes(input1)).digest()
)
hashlib_hash = bytes_to_native_str(hashlib_hash[:-1])
hashlib_hash = hashlib_hash[:-1].decode()
assert first_hash != second_hash
assert hashlib_hash == first_hash