findmy.keys#

Module to work with private and public keys as used in FindMy accessories.

Classes#

KeyType

Enum of possible key types.

KeyPairMapping

JSON mapping representing a KeyPair.

HasHashedPublicKey

ABC for anything that has a public, hashed FindMy-key.

HasPublicKey

ABC for anything that has a public FindMy-key.

KeyPair

A private-public keypair for a trackable FindMy accessory.

KeyGenerator

KeyPair generator.

Module Contents#

class findmy.keys.KeyType(*args, **kwds)#

Bases: enum.Enum

Inheritance diagram of findmy.keys.KeyType

Enum of possible key types.

UNKNOWN = 0#
PRIMARY = 1#
SECONDARY = 2#
class findmy.keys.KeyPairMapping#

Bases: TypedDict

Inheritance diagram of findmy.keys.KeyPairMapping

JSON mapping representing a KeyPair.

type: Literal['keypair']#
private_key: str#
key_type: int#
name: str | None#
class findmy.keys.HasHashedPublicKey#

Bases: abc.ABC

Inheritance diagram of findmy.keys.HasHashedPublicKey

ABC for anything that has a public, hashed FindMy-key.

Also called a “hashed advertisement” key or “lookup” key.

property hashed_adv_key_bytes: bytes#
Abstractmethod:

Return the hashed advertised (public) key as bytes.

property hashed_adv_key_b64: str#

Return the hashed advertised (public) key as a base64-encoded string.

__hash__() int#
__eq__(other: object) bool#
class findmy.keys.HasPublicKey#

Bases: HasHashedPublicKey, abc.ABC

Inheritance diagram of findmy.keys.HasPublicKey

ABC for anything that has a public FindMy-key.

Also called an “advertisement” key, since it is the key that is advertised by findable devices.

property adv_key_bytes: bytes#
Abstractmethod:

Return the advertised (public) key as bytes.

property adv_key_b64: str#

Return the advertised (public) key as a base64-encoded string.

property hashed_adv_key_bytes: bytes#

See HasHashedPublicKey.hashed_adv_key_bytes().

property mac_address: str#

Get the mac address from the public key.

adv_data(status: int = 0, hint: int = 0) bytes#

Get the BLE advertisement data that should be broadcast to advertise this key.

of_data(status: int = 0, hint: int = 0) bytes#

Get the Offline Finding data that should be broadcast to advertise this key.

class findmy.keys.KeyPair(private_key: bytes, key_type: KeyType = KeyType.UNKNOWN, name: str | None = None)#

Bases: HasPublicKey, findmy.util.abc.Serializable[KeyPairMapping]

Inheritance diagram of findmy.keys.KeyPair

A private-public keypair for a trackable FindMy accessory.

property key_type: KeyType#

Type of this key.

property name: str | None#

Name of this KeyPair.

classmethod new() KeyPair#

Generate a new random KeyPair().

classmethod from_b64(key_b64: str) KeyPair#

Import an existing KeyPair() from its base64-encoded representation.

Same format as returned by KeyPair.private_key_b64().

property private_key_bytes: bytes#

Return the private key as bytes.

property private_key_b64: str#

Return the private key as a base64-encoded string.

Can be re-imported using KeyPair.from_b64().

property adv_key_bytes: bytes#

Return the advertised (public) key as bytes.

to_json(dst: str | pathlib.Path | None = None, /) KeyPairMapping#

Export the current state of the object as a JSON-serializable dictionary.

If an argument is provided, the output will also be written to that file.

The output of this method is guaranteed to be JSON-serializable, and passing the return value of this function as an argument to Serializable.from_json() will always result in an exact copy of the internal state as it was when exported.

You are encouraged to save and load object states to and from disk whenever possible, to prevent unnecessary API calls or otherwise unexpected behavior.

classmethod from_json(val: str | pathlib.Path | KeyPairMapping, /) KeyPair#

Restore state from a previous Closable.to_json() export.

If given a str or Path, it must point to a json file from Serializable.to_json(). Otherwise, it should be the Mapping itself.

See Serializable.to_json() for more information.

dh_exchange(other_pub_key: cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey) bytes#

Do a Diffie-Hellman key exchange using another EC public key.

__repr__() str#
class findmy.keys.KeyGenerator#

Bases: abc.ABC, Generic[_K]

Inheritance diagram of findmy.keys.KeyGenerator

KeyPair generator.

abstractmethod __iter__() KeyGenerator#
abstractmethod __next__() _K#
abstractmethod __getitem__(val: int) _K#
abstractmethod __getitem__(val: slice) collections.abc.Generator[_K, None, None]