当前位置: 首页>>代码示例>>Python>>正文


Python serialization.load_der_private_key方法代码示例

本文整理汇总了Python中cryptography.hazmat.primitives.serialization.load_der_private_key方法的典型用法代码示例。如果您正苦于以下问题:Python serialization.load_der_private_key方法的具体用法?Python serialization.load_der_private_key怎么用?Python serialization.load_der_private_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cryptography.hazmat.primitives.serialization的用法示例。


在下文中一共展示了serialization.load_der_private_key方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: convert_der_to_pem

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def convert_der_to_pem(der_key, is_private=False):
    """
    Converts a given key from DER to PEM format.

    :param der_key: DER-encoded key bytes
    :type der_key: bytearray
    :param is_private: Whether the key is public or private. Default: False
    :type is_private: bool
    :return: PEM-encoded key bytes
    :rtype: bytearray
    """
    if is_private:
        loaded_key = crypto_serialization.load_der_private_key(der_key, backend=crypto_default_backend())
        return serialize_key(loaded_key, encoding='PEM', return_private=is_private)
    else:
        loaded_key = crypto_serialization.load_der_public_key(der_key, backend=crypto_default_backend())
        return loaded_key.public_bytes(encoding=crypto_serialization.Encoding.PEM,
                                       format=crypto_serialization.PublicFormat.SubjectPublicKeyInfo) 
开发者ID:aws,项目名称:aws-ec2-instance-connect-cli,代码行数:20,代码来源:key_utils.py

示例2: load

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def load(self, issuer):
        if not self._data_uri.startswith('data:application/pkcs8;kid='):
            raise PrivateKeyRetrieverException('Unrecognised data uri format.')
        splitted = self._data_uri.split(';')
        key_identifier = KeyIdentifier(unquote_plus(
            splitted[1][len('kid='):]))
        key_data = base64.b64decode(splitted[-1].split(',')[-1])
        key = serialization.load_der_private_key(
            key_data,
            password=None,
            backend=cryptography.hazmat.backends.default_backend())
        private_key_pem = key.private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.TraditionalOpenSSL,
            encryption_algorithm=serialization.NoEncryption()
        )
        return key_identifier, private_key_pem.decode('utf-8') 
开发者ID:atlassian,项目名称:asap-authentication-python,代码行数:19,代码来源:key.py

示例3: test_generate_1024_DSA_key_in_pem_and_reconstruct_key_der

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def test_generate_1024_DSA_key_in_pem_and_reconstruct_key_der(self):
        generate_dto = plugin.GenerateDTO('dsa', 1024, None, None)
        kek_meta_dto = self._get_mocked_kek_meta_dto()

        private_dto, public_dto, passwd_dto = self.plugin.generate_asymmetric(
            generate_dto,
            kek_meta_dto,
            mock.MagicMock()
        )

        decrypt_dto = plugin.DecryptDTO(private_dto.cypher_text)
        private_dto = self.plugin.decrypt(decrypt_dto,
                                          kek_meta_dto,
                                          private_dto.kek_meta_extended,
                                          mock.MagicMock())

        private_key = serialization.load_der_private_key(
            data=private_dto,
            password=None,
            backend=default_backend()
        )

        self.assertEqual(1024, private_key.key_size) 
开发者ID:openstack,项目名称:barbican,代码行数:25,代码来源:test_crypto.py

示例4: _getPrivateKeyObject

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def _getPrivateKeyObject(registry=None):
    registry = registry or Registry_Base_URL
    privatekey_pem = _getPrivateKey(registry)
    if not privatekey_pem:
        pubkey_pem, privatekey_pem = _generateAndSaveKeys(registry)
    else:
        # settings are unicode, we should be able to safely decode to ascii for
        # the key though, as it will either be hex or PEM encoded:
        privatekey_pem = privatekey_pem.encode('ascii')
    # if the key doesn't look like PEM, it might be hex-encided-DER (which we
    # used historically), so try loading that:
    if b'-----BEGIN PRIVATE KEY-----' in privatekey_pem:
        return serialization.load_pem_private_key(
            privatekey_pem, None, default_backend()
        )
    else:
        privatekey_der = binascii.unhexlify(privatekey_pem)
        return serialization.load_der_private_key(
            privatekey_der, None, default_backend()
        ) 
开发者ID:ARMmbed,项目名称:yotta,代码行数:22,代码来源:registry_access.py

示例5: authenticate

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def authenticate(
            self, authenticator, service_name, account, user, password):
        account = account.upper()
        user = user.upper()

        now = datetime.utcnow()

        try:
            private_key = load_der_private_key(data=self._private_key, password=None, backend=default_backend())
        except Exception as e:
            raise ProgrammingError(
                msg='Failed to load private key: {}\nPlease provide a valid unencrypted rsa private '
                    'key in DER format as bytes object'.format(str(e)),
                errno=ER_INVALID_PRIVATE_KEY
            )

        if not isinstance(private_key, RSAPrivateKey):
            raise ProgrammingError(
                msg='Private key type ({}) not supported.\nPlease provide a valid rsa private '
                    'key in DER format as bytes object'.format(private_key.__class__.__name__),
                errno=ER_INVALID_PRIVATE_KEY
            )

        public_key_fp = self.calculate_public_key_fingerprint(private_key)

        self._jwt_token_exp = now + self.LIFETIME
        payload = {
            self.ISSUER: "{}.{}.{}".format(account, user, public_key_fp),
            self.SUBJECT: "{}.{}".format(account, user),
            self.ISSUE_TIME: now,
            self.EXPIRE_TIME: self._jwt_token_exp
        }

        self._jwt_token = jwt.encode(payload, private_key,
                                     algorithm=self.ALGORITHM).decode('utf-8')

        return self._jwt_token 
开发者ID:snowflakedb,项目名称:snowflake-connector-python,代码行数:39,代码来源:auth_keypair.py

示例6: _get_normalized_payload

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def _get_normalized_payload(self, encoded_bytes, secret_type):
        """Normalizes the bytes of the object.

        Barbican expects certificates, public keys, and private keys in PEM
        format, but Castellan expects these objects to be DER encoded bytes
        instead.
        """
        if secret_type == 'public':
            key = serialization.load_der_public_key(
                encoded_bytes,
                backend=backends.default_backend())
            return key.public_bytes(
                encoding=serialization.Encoding.PEM,
                format=serialization.PublicFormat.SubjectPublicKeyInfo)
        elif secret_type == 'private':
            key = serialization.load_der_private_key(
                encoded_bytes,
                backend=backends.default_backend(),
                password=None)
            return key.private_bytes(
                encoding=serialization.Encoding.PEM,
                format=serialization.PrivateFormat.PKCS8,
                encryption_algorithm=serialization.NoEncryption())
        elif secret_type == 'certificate':
            cert = cryptography_x509.load_der_x509_certificate(
                encoded_bytes,
                backend=backends.default_backend())
            return cert.public_bytes(encoding=serialization.Encoding.PEM)
        else:
            return encoded_bytes 
开发者ID:openstack,项目名称:castellan,代码行数:32,代码来源:barbican_key_manager.py

示例7: get_cryptography_private_key

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def get_cryptography_private_key(private_key):
    crypto_private_key = serialization.load_der_private_key(
        bytes(private_key.get_encoded()),
        password=None,
        backend=backends.default_backend())
    return crypto_private_key 
开发者ID:openstack,项目名称:castellan,代码行数:8,代码来源:test_mock_key_manager.py

示例8: from_key_bytes

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def from_key_bytes(cls, algorithm, key_bytes):
        """Builds a `Signer` from an algorithm suite and a raw signing key.

        :param algorithm: Algorithm on which to base signer
        :type algorithm: aws_encryption_sdk.identifiers.Algorithm
        :param bytes key_bytes: Raw signing key
        :rtype: aws_encryption_sdk.internal.crypto.Signer
        """
        key = serialization.load_der_private_key(data=key_bytes, password=None, backend=default_backend())
        return cls(algorithm, key) 
开发者ID:aws,项目名称:aws-encryption-sdk-python,代码行数:12,代码来源:authentication.py

示例9: load_private_key

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def load_private_key(cls, cert_private_key: bytes, password, backend):
        return serialization.load_der_private_key(cert_private_key, password, backend) 
开发者ID:icon-project,项目名称:loopchain,代码行数:4,代码来源:der_serializer.py

示例10: import_from_asn1pkt

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def import_from_asn1pkt(self, privkey):
        self.key = serialization.load_der_private_key(raw(privkey), None,
                                                      backend=default_backend())  # noqa: E501
        self.pubkey = self.key.public_key() 
开发者ID:secdev,项目名称:scapy,代码行数:6,代码来源:cert.py

示例11: _create_RSA_private_key

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def _create_RSA_private_key(self,
                                bytes):
        """
        Instantiates an RSA key from bytes.

        Args:
            bytes (byte string): Bytes of RSA private key.
        Returns:
            private_key
                (cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey):
                RSA private key created from key bytes.
        """

        try:
            private_key = serialization.load_pem_private_key(
                bytes,
                password=None,
                backend=default_backend()
            )
            return private_key
        except Exception:
            private_key = serialization.load_der_private_key(
                bytes,
                password=None,
                backend=default_backend()
            )
            return private_key 
开发者ID:OpenKMIP,项目名称:PyKMIP,代码行数:29,代码来源:engine.py

示例12: load_private_key

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def load_private_key(key):
    try:
        return serialization.load_der_private_key(
            key,
            password=None,
            backend=default_backend()
        )
    except Exception:
        return serialization.load_pem_private_key(
            key,
            password=None,
            backend=default_backend()
        ) 
开发者ID:OpenKMIP,项目名称:PyKMIP,代码行数:15,代码来源:test_engine.py

示例13: rsa_from_der

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def rsa_from_der(rsa_der_data: bytes, password: str = None) -> rsa.RSAPrivateKeyWithSerialization:
    return serialization.load_der_private_key(
        rsa_der_data,
        password,
        default_backend()
    ) 
开发者ID:cmdmnt,项目名称:commandment,代码行数:8,代码来源:serialization.py

示例14: _find_rsa_key_length

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def _find_rsa_key_length(key):
    loaded_key = serialization.load_der_private_key(data=key, password=None, backend=default_backend())
    return loaded_key.key_size 
开发者ID:aws,项目名称:aws-dynamodb-encryption-python,代码行数:5,代码来源:test_jce.py

示例15: _decode_key

# 需要导入模块: from cryptography.hazmat.primitives import serialization [as 别名]
# 或者: from cryptography.hazmat.primitives.serialization import load_der_private_key [as 别名]
def _decode_key(self, data):
        try:
            key = serialization.load_der_private_key(
                data, password=None, backend=default_backend()
            )
        except (ValueError, AssertionError) as e:
            raise SSHException(str(e))

        self.signing_key = key
        self.verifying_key = key.public_key()
        curve_class = key.curve.__class__
        self.ecdsa_curve = self._ECDSA_CURVES.get_by_curve_class(curve_class) 
开发者ID:iopsgroup,项目名称:imoocc,代码行数:14,代码来源:ecdsakey.py


注:本文中的cryptography.hazmat.primitives.serialization.load_der_private_key方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。