本文整理汇总了Python中pyasn1_modules.pem.readPemBlocksFromFile方法的典型用法代码示例。如果您正苦于以下问题:Python pem.readPemBlocksFromFile方法的具体用法?Python pem.readPemBlocksFromFile怎么用?Python pem.readPemBlocksFromFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyasn1_modules.pem
的用法示例。
在下文中一共展示了pem.readPemBlocksFromFile方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_from_string_pkcs8_extra_bytes
# 需要导入模块: from pyasn1_modules import pem [as 别名]
# 或者: from pyasn1_modules.pem import readPemBlocksFromFile [as 别名]
def test_from_string_pkcs8_extra_bytes(self):
key_bytes = PKCS8_KEY_BYTES
_, pem_bytes = pem.readPemBlocksFromFile(
six.StringIO(_helpers.from_bytes(key_bytes)),
_python_rsa._PKCS8_MARKER)
key_info, remaining = None, 'extra'
decode_patch = mock.patch(
'pyasn1.codec.der.decoder.decode',
return_value=(key_info, remaining),
autospec=True)
with decode_patch as decode:
with pytest.raises(ValueError):
_python_rsa.RSASigner.from_string(key_bytes)
# Verify mock was called.
decode.assert_called_once_with(
pem_bytes, asn1Spec=_python_rsa._PKCS8_SPEC)
示例2: from_string
# 需要导入模块: from pyasn1_modules import pem [as 别名]
# 或者: from pyasn1_modules.pem import readPemBlocksFromFile [as 别名]
def from_string(cls, key, password='notasecret'):
"""Construct an RsaSigner instance from a string.
Args:
key: string, private key in PEM format.
password: string, password for private key file. Unused for PEM
files.
Returns:
RsaSigner instance.
Raises:
ValueError if the key cannot be parsed as PKCS#1 or PKCS#8 in
PEM format.
"""
key = _from_bytes(key) # pem expects str in Py3
marker_id, key_bytes = pem.readPemBlocksFromFile(
six.StringIO(key), _PKCS1_MARKER, _PKCS8_MARKER)
if marker_id == 0:
pkey = rsa.key.PrivateKey.load_pkcs1(key_bytes,
format='DER')
elif marker_id == 1:
key_info, remaining = decoder.decode(
key_bytes, asn1Spec=_PKCS8_SPEC)
if remaining != b'':
raise ValueError('Unused bytes', remaining)
pkey_info = key_info.getComponentByName('privateKey')
pkey = rsa.key.PrivateKey.load_pkcs1(pkey_info.asOctets(),
format='DER')
else:
raise ValueError('No key could be detected.')
return cls(pkey)
示例3: from_string
# 需要导入模块: from pyasn1_modules import pem [as 别名]
# 或者: from pyasn1_modules.pem import readPemBlocksFromFile [as 别名]
def from_string(cls, key, password='notasecret'):
"""Construct an RsaSigner instance from a string.
Args:
key: string, private key in PEM format.
password: string, password for private key file. Unused for PEM
files.
Returns:
RsaSigner instance.
Raises:
ValueError if the key cannot be parsed as PKCS#1 or PKCS#8 in
PEM format.
"""
key = _helpers._from_bytes(key) # pem expects str in Py3
marker_id, key_bytes = pem.readPemBlocksFromFile(
six.StringIO(key), _PKCS1_MARKER, _PKCS8_MARKER)
if marker_id == 0:
pkey = rsa.key.PrivateKey.load_pkcs1(key_bytes,
format='DER')
elif marker_id == 1:
key_info, remaining = decoder.decode(
key_bytes, asn1Spec=_PKCS8_SPEC)
if remaining != b'':
raise ValueError('Unused bytes', remaining)
pkey_info = key_info.getComponentByName('privateKey')
pkey = rsa.key.PrivateKey.load_pkcs1(pkey_info.asOctets(),
format='DER')
else:
raise ValueError('No key could be detected.')
return cls(pkey)
示例4: test_from_string_pkcs8_extra_bytes
# 需要导入模块: from pyasn1_modules import pem [as 别名]
# 或者: from pyasn1_modules.pem import readPemBlocksFromFile [as 别名]
def test_from_string_pkcs8_extra_bytes(self):
key_bytes = self._load_pkcs8_key_bytes()
_, pem_bytes = pem.readPemBlocksFromFile(
six.StringIO(_helpers._from_bytes(key_bytes)),
_pure_python_crypt._PKCS8_MARKER)
with mock.patch('pyasn1.codec.der.decoder.decode') as mock_decode:
key_info, remaining = None, 'extra'
mock_decode.return_value = (key_info, remaining)
with self.assertRaises(ValueError):
crypt.RsaSigner.from_string(key_bytes)
# Verify mock was called.
mock_decode.assert_called_once_with(
pem_bytes, asn1Spec=_pure_python_crypt._PKCS8_SPEC)
示例5: from_string
# 需要导入模块: from pyasn1_modules import pem [as 别名]
# 或者: from pyasn1_modules.pem import readPemBlocksFromFile [as 别名]
def from_string(cls, key, key_id=None):
"""Construct an Signer instance from a private key in PEM format.
Args:
key (str): Private key in PEM format.
key_id (str): An optional key id used to identify the private key.
Returns:
google.auth.crypt.Signer: The constructed signer.
Raises:
ValueError: If the key cannot be parsed as PKCS#1 or PKCS#8 in
PEM format.
"""
key = _helpers.from_bytes(key) # PEM expects str in Python 3
marker_id, key_bytes = pem.readPemBlocksFromFile(
six.StringIO(key), _PKCS1_MARKER, _PKCS8_MARKER)
# Key is in pkcs1 format.
if marker_id == 0:
private_key = rsa.key.PrivateKey.load_pkcs1(
key_bytes, format='DER')
# Key is in pkcs8.
elif marker_id == 1:
key_info, remaining = decoder.decode(
key_bytes, asn1Spec=_PKCS8_SPEC)
if remaining != b'':
raise ValueError('Unused bytes', remaining)
private_key_info = key_info.getComponentByName('privateKey')
private_key = rsa.key.PrivateKey.load_pkcs1(
private_key_info.asOctets(), format='DER')
else:
raise ValueError('No key could be detected.')
return cls(private_key, key_id=key_id)