def compare_hash(hash_a, hash_b):
    """
    Compares two hashes in constant time.
    """
    hash_a = hash_a if isinstance(hash_a, bytes) else hash_a.encode('utf-8')
    hash_b = hash_b if isinstance(hash_b, bytes) else hash_b.encode('utf-8')
    if len(hash_a) != len(hash_b):
        return False
    invalid_chars = 0
    for char_a, char_b in zip(hash_a, hash_b):
        if char_a != char_b:
            invalid_chars += 1
    return invalid_chars == 0
