import base64
import hashlib


def get_hashname(name):
    """
    A cluster hashname is the first 9 characters of a SHA256 digest encoded in base64

    This is used as a compact way to uniquely identify a cluster's Linode resources.
    It's also stateless! If a user loses their terraform file and wishes to
    delete a cluster they can still do so.
    """
    hashname = base64.b64encode(hashlib.sha256(name.encode('utf8')).digest())
    hashname = hashname.decode('utf8')
    hashname = hashname.replace('+', '')
    hashname = hashname.replace('/', '')
    return hashname[:9]