【问题标题】:python asn1 structure unable to set field valuespython asn1结构无法设置字段值
【发布时间】:2019-04-22 12:31:05
【问题描述】:

我正在尝试使用 pyasn1 在 python 3.7 中构建一个 asn1 结构来序列化 ECDSA 签名。

我定义了以下结构:(来自此处找到的示例http://snmplabs.com/pyasn1/

class ASNBitcoinSignature(Sequence):
    componentType = NamedTypes(
        NamedType('r', Integer()),
        NamedType('s', Integer()),
    )

我对签名的 r 和 s 值进行编码的代码如下所示:

asn = ASNBitcoinSignature()
asn['r'] = self.r().x()
asn['s'] = self.s()
serialized = encode(asn)

运行我收到的代码

'No field named "r" defined for ASNBitcoinSignature'

有人知道出了什么问题吗?这可能是一个旧示例,现在需要以其他方式设置或定义字段值吗?

【问题讨论】:

    标签: python asn.1


    【解决方案1】:

    此错误来自错误的序列导入:

    原来我是进口的

    from asn1crypto.core import Sequence
    

    但我真正想要的是

    from pyasn1.type.univ import Sequence
    

    此代码的另一个问题(仅供将来参考)是 pyasn1 中定义的 Integer 类型对于我的用例而言最大值太低。

    我可以通过像这样创建一个新的整数子类型来解决这个问题:

    class ASNBigInteger(Integer):
        subtypeSpec = ValueRangeConstraint(0x1, 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141)
    

    【讨论】:

    • 似乎对 pyasn1 Integer 没有范围限制。我想知道你是怎么打到的...
    猜你喜欢
    • 2012-03-22
    • 2019-02-06
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    相关资源
    最近更新 更多