Skip to content

Commit 5c117dd

Browse files
authored
Merge pull request #113 from shreyasminocha/pubkey-ble-conversion
Add BLE ad data generation method
2 parents 15bcf64 + 50bd844 commit 5c117dd

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

findmy/keys.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,43 @@ def adv_key_b64(self) -> str:
7474
"""Return the advertised (public) key as a base64-encoded string."""
7575
return base64.b64encode(self.adv_key_bytes).decode("ascii")
7676

77+
@property
78+
@override
79+
def hashed_adv_key_bytes(self) -> bytes:
80+
"""See `HasHashedPublicKey.hashed_adv_key_bytes`."""
81+
return hashlib.sha256(self.adv_key_bytes).digest()
82+
7783
@property
7884
def mac_address(self) -> str:
7985
"""Get the mac address from the public key."""
8086
first_byte = (self.adv_key_bytes[0] | 0b11000000).to_bytes(1)
8187
return ":".join([parsers.format_hex_byte(x) for x in first_byte + self.adv_key_bytes[1:6]])
8288

83-
@property
84-
@override
85-
def hashed_adv_key_bytes(self) -> bytes:
86-
"""See `HasHashedPublicKey.hashed_adv_key_bytes`."""
87-
return hashlib.sha256(self.adv_key_bytes).digest()
89+
def adv_data(self, status: int = 0, hint: int = 0) -> bytes:
90+
"""Get the BLE advertisement data that should be broadcast to advertise this key."""
91+
return bytes(
92+
[
93+
# apple company id
94+
0x4C,
95+
0x00,
96+
],
97+
) + self.of_data(status, hint)
98+
99+
def of_data(self, status: int = 0, hint: int = 0) -> bytes:
100+
"""Get the Offline Finding data that should be broadcast to advertise this key."""
101+
return bytes(
102+
[
103+
# offline finding
104+
0x12,
105+
# offline finding data length
106+
25,
107+
status,
108+
# remaining public key bytes
109+
*self.adv_key_bytes[6:],
110+
self.adv_key_bytes[0] >> 6,
111+
hint,
112+
],
113+
)
88114

89115

90116
class KeyPair(HasPublicKey):

0 commit comments

Comments
 (0)