import hashlib


def _SF_String__HashStr(string: str, algorithm: str) -> str:  # used by SF_String.HashStr() Basic method
    """
    Hash a given UTF-8 string with the given hashing algorithm
    Example
        hash = _SF_String__HashStr('This is a UTF-8 encoded string.','MD5')
    """
    algo = algorithm.lower()
    try:
        if algo in hashlib.algorithms_guaranteed:
            ENCODING = 'utf-8'
            bytestring = string.encode(ENCODING)    # Hashing functions expect bytes, not strings
            if algo == 'md5':
                hasher = hashlib.md5(bytestring)
            elif algo == 'sha1':
                hasher = hashlib.sha1(bytestring)
            elif algo == 'sha224':
                hasher = hashlib.sha224(bytestring)
            elif algo == 'sha256':
                hasher = hashlib.sha256(bytestring)
            elif algo == 'sha384':
                hasher = hashlib.sha384(bytestring)
            elif algo == 'sha512':
                hasher = hashlib.sha512(bytestring)
            else:
                return ''
            return hasher.hexdigest()
        else:
            return ''
    except Exception:
        return ''