2015-07-22 39 views
0

我發現在pyasn1中添加顯式標籤項的最佳方式是...明確標記它們。但是,這看起來過於冗長:在pyasn1中添加標籤項的簡單方法

cert['tbsCertificate']['extensions'] = rfc2459.Extensions().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)) 

有什麼辦法來產生一個空值將適用於像extensions的地方沒有指定的標籤?

回答

1

有更簡單的方法。慣例是,如果將None分配給複雜[py] ASN.1類型的組件,則該組件將被實例化,但不會有任何值。

>>> cert = rfc2459.Certificate() 
>>> print cert.prettyPrint() 
Certificate: 
>>> cert['tbsCertificate'] = None 
>>> print cert.prettyPrint() 
Certificate: 
tbsCertificate=TBSCertificate: 
>>> cert['tbsCertificate']['extensions'] = None 
>>> print cert.prettyPrint() 
Certificate: 
tbsCertificate=TBSCertificate: 
    extensions=Extensions: 
>>> cert['tbsCertificate']['extensions'][0] = None 
>>> print cert.prettyPrint() 
Certificate: 
tbsCertificate=TBSCertificate: 
    extensions=Extensions: 
    Extension: 
>>> cert['tbsCertificate']['extensions'][0]['extnID'] = '1.3.5.4.3.2' 
>>> cert['tbsCertificate']['extensions'][0]['extnValue'] = '\x00\x00' 
>>> print cert.prettyPrint() 
Certificate: 
tbsCertificate=TBSCertificate: 
    extensions=Extensions: 
    Extension: 
    extnID=1.3.5.4.3.2 
    extnValue=0x0000 
>>> 

這實際上讓您建立從複合pyasn1對象或者Python中內置或其他步驟pyasn1對象,而無需重複其類型規範。

相關問題