2023-01-17 23:54:27 +00:00
|
|
|
from typing import TYPE_CHECKING, Union
|
|
|
|
|
|
|
|
from ..identity import Identity
|
|
|
|
|
2023-01-20 23:22:38 +00:00
|
|
|
import result
|
|
|
|
import nacl.exceptions
|
2023-01-17 23:54:27 +00:00
|
|
|
import nacl.public
|
|
|
|
import nacl.utils
|
|
|
|
|
|
|
|
|
2023-01-20 23:22:38 +00:00
|
|
|
@result.as_result(nacl.exceptions.CryptoError, AttributeError)
|
2023-01-17 23:54:27 +00:00
|
|
|
def encrypt_to_identity_anonymously(
|
|
|
|
identity: 'Identity',
|
|
|
|
message: Union[bytes, str]) -> nacl.utils.EncryptedMessage:
|
2023-01-20 23:22:38 +00:00
|
|
|
|
2023-01-17 23:54:27 +00:00
|
|
|
their_public_key = identity.key.to_curve25519_public_key()
|
|
|
|
box = nacl.public.SealedBox(their_public_key)
|
|
|
|
|
|
|
|
try:
|
|
|
|
message = message.encode('utf-8')
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return box.encrypt(message)
|
|
|
|
|
|
|
|
|
2023-01-20 23:22:38 +00:00
|
|
|
@result.as_result(nacl.exceptions.CryptoError, AttributeError)
|
2023-01-17 23:54:27 +00:00
|
|
|
def decrypt_from_identity_anonymously(
|
|
|
|
our_identity: 'Identity', message: bytes) -> bytes:
|
2023-01-20 23:22:38 +00:00
|
|
|
|
2023-01-17 23:54:27 +00:00
|
|
|
our_private_key = our_identity.private_key.to_curve25519_private_key()
|
|
|
|
box = nacl.public.SealedBox(our_private_key)
|
|
|
|
|
|
|
|
return box.decrypt(message)
|
|
|
|
|
|
|
|
|
2023-01-20 23:22:38 +00:00
|
|
|
@result.as_result(nacl.exceptions.CryptoError, AttributeError)
|
2023-01-17 23:54:27 +00:00
|
|
|
def encrypt_to_identity(
|
|
|
|
our_identity: 'Identity',
|
|
|
|
identity: 'Identity',
|
|
|
|
message: Union[bytes, str]) -> nacl.utils.EncryptedMessage:
|
|
|
|
our_private_key = our_identity.private_key.to_curve25519_private_key()
|
|
|
|
their_public_key = identity.key.to_curve25519_public_key()
|
|
|
|
box = nacl.public.Box(our_private_key, their_public_key)
|
|
|
|
|
|
|
|
try:
|
|
|
|
message = message.encode('utf-8')
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return box.encrypt(message)
|
|
|
|
|
2023-01-20 23:22:38 +00:00
|
|
|
@result.as_result(nacl.exceptions.CryptoError, AttributeError)
|
2023-01-17 23:54:27 +00:00
|
|
|
def decrypt_from_identity(
|
|
|
|
our_identity: 'Identity',
|
|
|
|
identity: 'Identity',
|
|
|
|
message: bytes) -> bytes:
|
|
|
|
our_private_key = our_identity.private_key.to_curve25519_private_key()
|
|
|
|
their_public_key = identity.key.to_curve25519_public_key()
|
|
|
|
box = nacl.public.Box(our_private_key, their_public_key)
|
|
|
|
|
|
|
|
return box.decrypt(message)
|