我正在使用加密技术加载新证书并检索其属性。
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')>])>
如何正确访问这些属性之一?
例如,我只想打印 sha256WithRSAEncryption
和 C=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
关于Python:检索 <ObjectIdentifier> 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54910839/