8 from hashlib import blake2s
11 from pyblake2 import blake2s
13 print('No native BLAKE2 support; please pip install pyblake2')
16 LL_NET_PART = int(ipaddress.IPv6Address('fe80::'))
18 def pubkey_to_lladdr(pubkey):
19 # Check / normalize public key to raw bytes.
20 if isinstance(pubkey, str):
21 pubkey = base64.b64decode(pubkey)
22 elif isinstance(pubkey, bytes):
24 pubkey = base64.b64decode(pubkey)
27 assert len(pubkey) == 32
29 # Hash and generate interface part
30 pk_hash = blake2s(pubkey, digest_size=8)
31 node = int.from_bytes(pk_hash.digest(), 'big')
33 # Combine with link-local net part.
34 addr = ipaddress.IPv6Address(LL_NET_PART + node)
39 addr = pubkey_to_lladdr(pubkey)
42 if __name__ == '__main__':