import hmac
import hashlib


def generate_dendrite_mac(shared_secret: str, username: str, password: str, admin: bool) -> str:
    """
    Generate a MAC for using in registering users with Dendrite.
    """
    # From: https://github.com/matrix-org/dendrite/blob/master/clientapi/routing/register.go
    mac = hmac.new(
      key=shared_secret.encode('utf8'),
      digestmod=hashlib.sha1,
    )

    mac.update(username.encode('utf8'))
    mac.update(b"\x00")
    mac.update(password.encode('utf8'))
    mac.update(b"\x00")
    mac.update(b"admin" if admin else b"notadmin")
    return mac.hexdigest()