import hmac
import hashlib
import base64


def _prepend_signature(app_secret, payload):
        """
            Returns a SHA256 signed and base64 encoded signature based on the given payload

            Arguments:
            app_secret -- the secret key that Facebook assigns to each Facebook application
            payload -- a base64url encoded String
        """
        dig = hmac.new(app_secret, msg=payload, digestmod=hashlib.sha256).digest()
        dig = base64.urlsafe_b64encode(dig)
        return dig