import time
import hmac
import hashlib
import base64


def get_secure_link(url, key, expire=60, t=None):
    """Sign url or POST data with hmac

    :param str url: The url to sign
    :param str key: The shared secret
    :param int expire: Time in seconds until link expires
    :param int t: The current timestamp (utc)
    :rtype: str
    """
    if u'?' in url:
        url += u'&'
    else:
        url += u'?'
    if t is None:
        t = int(time.time())

    expire += t
    url += u'e=' + str(expire)
    s = hmac.new(key.encode(), url.encode(), hashlib.sha256).digest()
    return url + u'&s=' + base64.b64encode(s, b'-_').decode().rstrip(u'=')