def hash(string, hash, key=None, digest_size=None):
    """
    Gets the hash of a string, based on the key. You can specify a digest_size (the length
    of the encoded string) but the key and digest are optional.
    If you are using either blake2b or blake2s, you will need both key and digest_size.
    You must choose a hash (md5, sha256, sha224, ripemd160, blake2b, blake2s or sha512)

    Usage (no key or digest_size):
    >>> vdm.hash('mypassword', 'md5')
    '34819d7beeabb9260a5c854bc85b3e44'
    >>>
    """
    contents = bytes(string, encoding='utf-8')
    import hashlib
    if hash == 'md5':
        re = hashlib.md5(contents)
        return re.hexdigest()
    elif hash == 'sha256':
        returnEncoded = hashlib.sha256()
        returnEncoded.update(contents)
        return returnEncoded.hexdigest()
    elif hash == 'sha224':
        returnEncoded = hashlib.sha224(contents)
        return returnEncoded.hexdigest()
    elif hash == 'ripemd160':
        returnEncoded = hashlib.new('ripemd160')
        returnEncoded.update(contents)
        return returnEncoded.hexdigest()
    elif hash == 'blake2b':
        h = hashlib.blake2b(key=key, digest_size=digest_size)
        h.update(contents)
        return h.hexdigest()
    elif hash == 'sha512':
        h = hashlib.sha512()
        h.update(contents)
        return h.hexdigest()
    elif hash == 'blake2s':
        h = hashlib.blake2s(key=key, digest_size=digest_size)
        h.update(contents)
        return h.hexdigest()
    else:
        raise SyntaxError(
            'Specify a valid hash (md5, sha256, sha224, ripemd160, blake2b, blake2s or sha512)'
            )
