Python:检索 <ObjectIdentifier> 的属性

标签 python cryptography

我正在使用加密技术加载新证书并检索其属性。

from cryptography import x509
from cryptography.hazmat.backends import default_backend
path = "mycert.crt"
with open(path, 'rb') as cert_file:
    data = cert_file.read()
cert = x509.load_pem_x509_certificate(data, default_backend())
sign = cert.signature_algorithm_oid
iss = cert.issuer

最后两个打印结果为:

print sign
<ObjectIdentifier(oid=1.1.1.1.1.1.11, name=sha256WithRSAEncryption)>

print iss
<Name([<NameAttribute(oid=<ObjectIdentifier(oid=1.1.1.1, name=countryName)>, value=u'GR')>, <NameAttribute(oid=<ObjectIdentifier(oid=1.1.1.1, name=organizationName)>, value=u'MyTrust')>, <NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value=u'MyTrust')>])>

如何正确访问这些属性之一? 例如,我只想打印 sha256WithRSAEncryptionC=GR, O=MyTrust, CN=MyTrust

在 Danielle 回复后,我正在更新问题。

>>> dir(iss)   
>>> ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__len__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_attributes', 'get_attributes_for_oid', 'public_bytes', 'rdns']

当我打印一份时,它仍然很难访问:

>>> print(iss.__dict__)  
>>> {'_attributes': [<RelativeDistinguishedName([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.6, name=countryName)>, value=u'GR')>])>, <RelativeDistinguishedName([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.10, name=organizationName)>, value=u'MyTrust')>])>, <RelativeDistinguishedName([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.11, name=organizationalUnitName)>, value=u'MyTrust')>])>, <RelativeDistinguishedName([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value=u'MyTrust')>])>]}

如何获取所有值? 国家的名字 机构名称 组织单位名称 通用名 等等

最佳答案

如果您想检查一个对象并找到它的属性,请尝试使用 dir() 函数:

>>> dir(sign)
>>> ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_dotted_string', '_name', 'dotted_string']

也许_name

>>> print(sign._name)
>>> sha1WithRSAEncryption

https://docs.python.org/2/library/functions.html#dir

关于Python:检索 <ObjectIdentifier> 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54910839/

相关文章:

python - 在TensorFlow中,字符串常量的打印总是附有 'b'

python - IPython - 循环输入历史记录中的 block ?

python - 计算两个日期之间的星期几小时对

java - 读取 Unix 中服务交换的 key

c# - 对象已存在于 RSACryptoServiceProvider 中

Python/Scipy - 与 Quad Along Axis 集成

python - 将 float 附加到日期列表

c# - 如何从 AES 256 加密文件中获取文件类型

c# - Java BouncyCaSTLe AES 解密的 C# 等价物是什么?

c# - 创建证书时未处理的加密异常