import hashlib, getpass


def enter_password(prompt='Password: ', hash_method=None):
    """
    Turns off echo and asks user password.

    Parameters
    ----------
    prompt : str
        A prompt message to show.
    hash_method : str or None
        If specified, hash the password and return the hash. Otherwise
        just return plain password. Supported methods are listed in the
        "algorithms_guaranteed" variable in hashlib module.

    Returns
    -------
    password : str
        Plaintext or hashed user input.
    """

    password = getpass.getpass(prompt=prompt)
    if isinstance(hash_method, str):
        if hash_method not in hashlib.algorithms_guaranteed:
            raise ValueError('Unsupported hash method: %s' % hash_method)
        hash_method = getattr(hashlib, hash_method)
        hashobj = hash_method(password.encode())
        password = hashobj.hexdigest()
    elif hash_method is not None:
        raise ValueError("Parameter 'hash_method' must be a NoneType or str!")
    return password