python asn1结构无法设置字段值

2024-06-23 19:42:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在python3.7中构建一个asn1结构,使用pyasn1序列化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'

有人知道怎么了吗?这可能是一个旧的例子,字段值现在需要以另一种方式设置或定义吗?你知道吗


Tags: 代码selfhttp序列化定义integer结构ecdsa
1条回答
网友
1楼 · 发布于 2024-06-23 19:42:25

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

原来我是进口的

from asn1crypto.core import Sequence

但我真正想要的是

from pyasn1.type.univ import Sequence

此代码的另一个问题(仅供将来参考)是pyasn1中定义的整数类型的最大值对于我的用例来说太低。你知道吗

我可以通过创建一个新的整数子类型来解决这个问题,如下所示:

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

相关问题 更多 >

    热门问题