import secrets


def genpw(length, encoder):
    """
    Generate a random password.

    Arguments:
        length: Length of the requested password.
        encoder: function to encode the bytes data.

    Returns:
        A password string.
    """
    d = secrets.token_bytes(2*length)
    return encoder(d).decode()[:length]