import hashlib
import json


def get_app_hash(raw_cwl):
    """If the CWL has a field "sbg:hash" (as used by sevenbridges-cwl) use that,
    else compute it (again, just like sevenbridges-cwl)

    :param raw_cwl:
    :return:
    """
    if "sbg:hash" in raw_cwl:
        return raw_cwl["sbg:hash"]
    else:
        sha = hashlib.sha512()
        sha.update(json.dumps(raw_cwl, sort_keys=True).encode('utf-8'))
        return sha.hexdigest()