#!/usr/bin/env python import base64 import ipaddress import sys try: from hashlib import blake2s except ImportError: try: from pyblake2 import blake2s except ImportError: print('No native BLAKE2 support; please pip install pyblake2') raise LL_NET_PART = int(ipaddress.IPv6Address('fe80::')) def pubkey_to_lladdr(pubkey): # Check / normalize public key to raw bytes. if isinstance(pubkey, str): pubkey = base64.b64decode(pubkey) elif isinstance(pubkey, bytes): if len(pubkey) != 32: pubkey = base64.b64decode(pubkey) else: raise TypeError() assert len(pubkey) == 32 # Hash and generate interface part pk_hash = blake2s(pubkey, digest_size=8) node = int.from_bytes(pk_hash.digest(), 'big') # Combine with link-local net part. addr = ipaddress.IPv6Address(LL_NET_PART + node) return addr def main(): pubkey = sys.argv[1] addr = pubkey_to_lladdr(pubkey) print(str(addr)) if __name__ == '__main__': main()