Python OpenSSL.crypto.sign() Examples

The following are 30 code examples of OpenSSL.crypto.sign(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module OpenSSL.crypto , or try the search function .
Example #1
Source File: core.py    From pyaci with Apache License 2.0 6 votes vote down vote up
def _x509Prep(self, rootApi, req, data):
        if rootApi._x509Key is None:
            return
        payload = '{}{}'.format(req.method, req.url.replace(rootApi._url, ''))
        payload = unquote(payload)
        if data is not None:
            payload += data
        signature = base64.b64encode(sign(rootApi._x509Key, payload,
                                          'sha256'))
        if sys.version_info[0] >= 3:
            signature = signature.decode('ascii')

        cookie = ('APIC-Request-Signature={}; '
                  'APIC-Certificate-Algorithm=v1.0; '
                  'APIC-Certificate-Fingerprint=fingerprint; '
                  'APIC-Certificate-DN={}').format(
                      signature, rootApi._x509Dn)
        req.headers['Cookie'] = cookie 
Example #2
Source File: sso.py    From vsphere-automation-sdk-python with MIT License 6 votes vote down vote up
def _sign(private_key, data, digest=SHA256):
    '''
    An internal helper method to sign the 'data' with the 'private_key'.

    @type  private_key: C{str}
    @param private_key: The private key used to sign the 'data', in one of
                        supported formats.
    @type         data: C{str}
    @param        data: The data that needs to be signed.
    @type       digest: C{str}
    @param      digest: Digest is a str naming a supported message digest type,
                        for example 'sha256'.

    @rtype: C{str}
    @return: Signed string.
    '''
    # Convert private key in arbitrary format into DER (DER is binary format
    # so we get rid of \n / \r\n differences, and line breaks in PEM).
    pkey = _load_private_key(_extract_certificate(private_key))
    return base64.b64encode(crypto.sign(pkey, data, digest)) 
Example #3
Source File: _openssl_crypt.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def sign(self, message):
        """Signs a message.

        Args:
            message: bytes, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        """
        message = _helpers._to_bytes(message, encoding='utf-8')
        return crypto.sign(self._key, message, 'sha256') 
Example #4
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def make_signed_jwt(signer, payload):
  """Make a signed JWT.

  See http://self-issued.info/docs/draft-jones-json-web-token.html.

  Args:
    signer: crypt.Signer, Cryptographic signer.
    payload: dict, Dictionary of data to convert to JSON and then sign.

  Returns:
    string, The JWT for the payload.
  """
  header = {'typ': 'JWT', 'alg': 'RS256'}

  segments = [
          _urlsafe_b64encode(_json_encode(header)),
          _urlsafe_b64encode(_json_encode(payload)),
  ]
  signing_input = '.'.join(segments)

  signature = signer.sign(signing_input)
  segments.append(_urlsafe_b64encode(signature))

  logger.debug(str(segments))

  return '.'.join(segments) 
Example #5
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey 
Example #6
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey 
Example #7
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey 
Example #8
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def sign(self, message):
      """Signs a message.

      Args:
        message: string, Message to be signed.

      Returns:
        string, The signature of the message for the given key.
      """
      return PKCS1_v1_5.new(self._key).sign(SHA256.new(message)) 
Example #9
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def make_signed_jwt(signer, payload):
  """Make a signed JWT.

  See http://self-issued.info/docs/draft-jones-json-web-token.html.

  Args:
    signer: crypt.Signer, Cryptographic signer.
    payload: dict, Dictionary of data to convert to JSON and then sign.

  Returns:
    string, The JWT for the payload.
  """
  header = {'typ': 'JWT', 'alg': 'RS256'}

  segments = [
          _urlsafe_b64encode(_json_encode(header)),
          _urlsafe_b64encode(_json_encode(payload)),
  ]
  signing_input = '.'.join(segments)

  signature = signer.sign(signing_input)
  segments.append(_urlsafe_b64encode(signature))

  logger.debug(str(segments))

  return '.'.join(segments) 
Example #10
Source File: _openssl_crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def sign(self, message):
        """Signs a message.

        Args:
            message: bytes, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        """
        message = _to_bytes(message, encoding='utf-8')
        return crypto.sign(self._key, message, 'sha256') 
Example #11
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey 
Example #12
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def sign(self, message):
      """Signs a message.

      Args:
        message: string, Message to be signed.

      Returns:
        string, The signature of the message for the given key.
      """
      return crypto.sign(self._key, message, 'sha256') 
Example #13
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey 
Example #14
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def sign(self, message):
      """Signs a message.

      Args:
        message: string, Message to be signed.

      Returns:
        string, The signature of the message for the given key.
      """
      return PKCS1_v1_5.new(self._key).sign(SHA256.new(message)) 
Example #15
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def sign(self, message):
      """Signs a message.

      Args:
        message: string, Message to be signed.

      Returns:
        string, The signature of the message for the given key.
      """
      return crypto.sign(self._key, message, 'sha256') 
Example #16
Source File: _openssl_crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def sign(self, message):
        """Signs a message.

        Args:
            message: bytes, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        """
        message = _to_bytes(message, encoding='utf-8')
        return crypto.sign(self._key, message, 'sha256') 
Example #17
Source File: _openssl_crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey 
Example #18
Source File: crypt.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey 
Example #19
Source File: _openssl_crypt.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey 
Example #20
Source File: crypt.py    From twitter-for-bigquery with Apache License 2.0 5 votes vote down vote up
def make_signed_jwt(signer, payload):
  """Make a signed JWT.

  See http://self-issued.info/docs/draft-jones-json-web-token.html.

  Args:
    signer: crypt.Signer, Cryptographic signer.
    payload: dict, Dictionary of data to convert to JSON and then sign.

  Returns:
    string, The JWT for the payload.
  """
  header = {'typ': 'JWT', 'alg': 'RS256'}

  segments = [
          _urlsafe_b64encode(_json_encode(header)),
          _urlsafe_b64encode(_json_encode(payload)),
  ]
  signing_input = '.'.join(segments)

  signature = signer.sign(signing_input)
  segments.append(_urlsafe_b64encode(signature))

  logger.debug(str(segments))

  return '.'.join(segments) 
Example #21
Source File: crypt.py    From twitter-for-bigquery with Apache License 2.0 5 votes vote down vote up
def sign(self, message):
      """Signs a message.

      Args:
        message: string, Message to be signed.

      Returns:
        string, The signature of the message for the given key.
      """
      return PKCS1_v1_5.new(self._key).sign(SHA256.new(message)) 
Example #22
Source File: crypt.py    From twitter-for-bigquery with Apache License 2.0 5 votes vote down vote up
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey 
Example #23
Source File: crypt.py    From twitter-for-bigquery with Apache License 2.0 5 votes vote down vote up
def sign(self, message):
      """Signs a message.

      Args:
        message: string, Message to be signed.

      Returns:
        string, The signature of the message for the given key.
      """
      return crypto.sign(self._key, message, 'sha256') 
Example #24
Source File: crypt.py    From twitter-for-bigquery with Apache License 2.0 5 votes vote down vote up
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey 
Example #25
Source File: _openssl_crypt.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def sign(self, message):
        """Signs a message.

        Args:
            message: bytes, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        """
        message = _to_bytes(message, encoding='utf-8')
        return crypto.sign(self._key, message, 'sha256') 
Example #26
Source File: _openssl_crypt.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey 
Example #27
Source File: _openssl_crypt.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def sign(self, message):
        """Signs a message.

        Args:
            message: bytes, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        """
        message = _to_bytes(message, encoding='utf-8')
        return crypto.sign(self._key, message, 'sha256') 
Example #28
Source File: _openssl_crypt.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey 
Example #29
Source File: _openssl_crypt.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey 
Example #30
Source File: _openssl_crypt.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def sign(self, message):
        """Signs a message.

        Args:
            message: bytes, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        """
        message = _to_bytes(message, encoding='utf-8')
        return crypto.sign(self._key, message, 'sha256')