"""Public key utilities."""

import base64
import binascii
import hashlib


def sha256_fingerprint(public_key: str) -> str:
    """SHA256 fingerprint of the public key."""
    data = public_key.split(" ")[1].encode()

    digest = hashlib.sha256(binascii.a2b_base64(data)).digest()
    encoded = base64.b64encode(digest).rstrip(b"=")  # ssh-keygen strips these
    return "SHA256:" + encoded.decode()
