def sign_data()

in asfpy/crypto.py [0:0]


    def sign_data(self, data: str = "", output_b64=False):
        """Signs a string with the private key for authenticity purposes.
        The signature includes a nonce for randomizing the response and returns three lines, split by newline:
        data-plus-nonce-signature
        nonce
        data

        The blob can be verified by verify_data, which will return the verified data if the signature is valid,
        else None.

        If output_b64 is True, the signed data is base64-encoded and returned as a single line. This can be
        useful for HTTP-based access tokens.
        """
        nonce = secrets.token_hex(32)
        data_plus_nonce = "\n".join([nonce, data])
        data_signature = self._privkey.sign(data_plus_nonce.encode("us-ascii"))
        response = "\n".join([base64.b64encode(data_signature).decode("us-ascii"), nonce, data])
        if output_b64:
            response = base64.b64encode(response.encode('us-ascii')).decode('us-ascii')
        return response