Python OpenSSL.crypto 模块,sign() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用OpenSSL.crypto.sign()

项目:munki-enrollment-client    作者:gerritdewitt    | 项目源码 | 文件源码
def sign_message(given_message,given_key):
    '''Signs the (hash of the) given message with the given private key.
        Returns the base64 encoded signature or or a blank string if something bad happened.'''
    # Check for blank message:
    if not given_message:
        common.print_error("Cannot sign blank message.")
        return None
    # Sign the message by encrypting its hash with the private key:
    try:
        signature = crypto.sign(given_key,given_message,'sha512')
        signature = base64.b64encode(signature)
    except crypto.Error:
        common.print_error("Error signing message!")
        signature = ''
    # Return signature:
    return signature
项目:mac-admin    作者:jacobfgrant    | 项目源码 | 文件源码
def assemble_cloudfront_request(resource, key, access_id, expires):
    """Assemble a CloudFront request."""
    # Format a request policy for the resource
    request_policy = {
        "Statement": [{"Resource": resource, "Condition": {"DateLessThan":
                      {"AWS:EpochTime": expires}}}]
    }
    request_policy = json.dumps(request_policy).replace(' ', '')
    # Sign and encode request policy
    signature = base64.b64encode(sign(key, request_policy, 'RSA-SHA1'))
    # Replace unsafe characters
    signature = signature.translate(maketrans('+=/', '-_~'))
    # Format the final request URL
    cloudfront_request = ("{0}?Expires={1}&Signature={2}&Key-Pair-Id={3}"
                          .format(resource, expires, signature, access_id))
    return cloudfront_request
项目:vsphere-automation-sdk-python    作者:vmware    | 项目源码 | 文件源码
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))
项目:deb-python-pyvmomi    作者:openstack    | 项目源码 | 文件源码
def _sign(private_key, data):
    '''
    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.

    @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, 'sha256'))
项目:Docker-XX-Net    作者:kuanghy    | 项目源码 | 文件源码
def test_sign(self):
        """
        L{X509Req.sign} succeeds when passed a private key object and a valid
        digest function.  C{X509Req.verify} can be used to check the signature.
        """
        request = self.signable()
        key = PKey()
        key.generate_key(TYPE_RSA, 512)
        request.set_pubkey(key)
        request.sign(key, 'MD5')
        # If the type has a verify method, cover that too.
        if getattr(request, 'verify', None) is not None:
            pub = request.get_pubkey()
            self.assertTrue(request.verify(pub))
            # Make another key that won't verify.
            key = PKey()
            key.generate_key(TYPE_RSA, 512)
            self.assertRaises(Error, request.verify, key)
项目:oscars2016    作者:0x0ece    | 项目源码 | 文件源码
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey
项目:oscars2016    作者:0x0ece    | 项目源码 | 文件源码
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')
项目:sndlatr    作者:Schibum    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:sndlatr    作者:Schibum    | 项目源码 | 文件源码
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')
项目:sndlatr    作者:Schibum    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:sndlatr    作者:Schibum    | 项目源码 | 文件源码
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))
项目:sndlatr    作者:Schibum    | 项目源码 | 文件源码
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)
项目:GAMADV-XTD    作者:taers232c    | 项目源码 | 文件源码
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey
项目:GAMADV-XTD    作者:taers232c    | 项目源码 | 文件源码
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')
项目:office-interoperability-tools    作者:milossramek    | 项目源码 | 文件源码
def __init__(self, pkey):
    """Constructor.

    Args:
      pkey, OpenSSL.crypto.PKey, The private key to sign with.
    """
    self._key = pkey
项目:office-interoperability-tools    作者:milossramek    | 项目源码 | 文件源码
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')
项目:office-interoperability-tools    作者:milossramek    | 项目源码 | 文件源码
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)
项目:iconograph    作者:robot-tools    | 项目源码 | 文件源码
def Wrap(self, instr):
    inbytes = instr.encode('utf8')
    return {
        'cert': self._cert_str,
        'other_certs': self._other_cert_strs,
        'sig': codecs.encode(crypto.sign(self._key, inbytes, 'sha256'), 'hex').decode('ascii'),
        'inner': instr,
    }
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
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.
      """
      from OpenSSL import crypto
      if isinstance(message, six.text_type):
        message = message.encode('utf-8')
      return crypto.sign(self._key, message, 'sha256')
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
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.
      """
      if isinstance(message, six.text_type):
        message = message.encode('utf-8')
      return PKCS1_v1_5.new(self._key).sign(SHA256.new(message))
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
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)
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
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.
      """
      from OpenSSL import crypto
      if isinstance(message, six.text_type):
        message = message.encode('utf-8')
      return crypto.sign(self._key, message, 'sha256')
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
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.
      """
      if isinstance(message, six.text_type):
        message = message.encode('utf-8')
      return PKCS1_v1_5.new(self._key).sign(SHA256.new(message))
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
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)
项目:deb-python-oauth2client    作者:openstack    | 项目源码 | 文件源码
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey
项目:deb-python-oauth2client    作者:openstack    | 项目源码 | 文件源码
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')
项目:OctoPrint-PolarCloud    作者:markwal    | 项目源码 | 文件源码
def _hello(self):
        self._logger.debug('hello')
        if self._serial and self._challenge:
            self._hello_sent = True
            self._status_now = True
            self._logger.debug('emit hello')
            self._printer_type = self._settings.get(["printer_type"])
            camUrl = self._settings.global_get(["webcam", "stream"])
            try:
                if camUrl:
                    camUrl = normalize_url(camUrl)
            except:
                self._logger.exception("Unable to canonicalize the url {}".format(camUrl))
            self._logger.debug("camUrl: {}".format(camUrl))
            transformImg = 0
            if self._settings.global_get(["webcam", "flipH"]):
                transformImg += 1
            if self._settings.global_get(["webcam", "flipV"]):
                transformImg += 2
            if self._settings.global_get(["webcam", "rotate90"]):
                transformImg += 4
            self._socket.emit('hello', {
                'serialNumber': self._serial,
                'signature': base64.b64encode(crypto.sign(self._key, self._challenge, b'sha256')),
                'MAC': get_mac(),
                'localIP': get_ip(),
                'protocol': '2',
                'camUrl': camUrl,
                'transformImg': transformImg,
                'printerType': self._printer_type
            })
            self._challenge = None
        else:
            self._logger.debug('skip emit hello, serial: {}'.format(self._serial))

    #~~ capabilities -> polar: capabilitiesResponse
项目:REMAP    作者:REMAPApp    | 项目源码 | 文件源码
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey
项目:REMAP    作者:REMAPApp    | 项目源码 | 文件源码
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')
项目:REMAP    作者:REMAPApp    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:REMAP    作者:REMAPApp    | 项目源码 | 文件源码
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')
项目:REMAP    作者:REMAPApp    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:REMAP    作者:REMAPApp    | 项目源码 | 文件源码
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)
项目:ecodash    作者:Servir-Mekong    | 项目源码 | 文件源码
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey
项目:ecodash    作者:Servir-Mekong    | 项目源码 | 文件源码
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')
项目:ecodash    作者:Servir-Mekong    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:ecodash    作者:Servir-Mekong    | 项目源码 | 文件源码
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')
项目:ecodash    作者:Servir-Mekong    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:ecodash    作者:Servir-Mekong    | 项目源码 | 文件源码
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)
项目:OneClickDTU    作者:satwikkansal    | 项目源码 | 文件源码
def __init__(self, pkey):
        """Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        """
        self._key = pkey
项目:OneClickDTU    作者:satwikkansal    | 项目源码 | 文件源码
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')
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
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.
      """
      from OpenSSL import crypto
      if isinstance(message, six.text_type):
        message = message.encode('utf-8')
      return crypto.sign(self._key, message, 'sha256')
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def __init__(self, pkey):
      """Constructor.

      Args:
        pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
      """
      self._key = pkey
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
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.
      """
      if isinstance(message, six.text_type):
        message = message.encode('utf-8')
      return PKCS1_v1_5.new(self._key).sign(SHA256.new(message))
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
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)