【问题标题】:Simpler way to add tagged items in pyasn1在 pyasn1 中添加标记项目的更简单方法
【发布时间】:2015-07-22 03:49:55
【问题描述】:

我发现在 pyasn1 中添加显式标记项目的最佳方法是...显式标记它们。但这看起来过于冗长:

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

有没有什么方法可以在不指定标签的情况下生成一个适合extensions 之类的地方的空值?

【问题讨论】:

    标签: python asn.1 pyasn1


    【解决方案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
    >>> 
    

    这可以有效地让您从 Python 内置对象或其他 pyasn1 对象逐步构建复合 pyasn1 对象,而无需重复其类型规范。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-15
      • 2015-06-15
      • 2011-05-25
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多