我正在尝试构建一个ASN.1类型,其中包含一个可变大小的整数列表,并像这样尝试过
class ASNBigInteger(Integer):
"""
A subtype of the pyasn1 Integer type to support
bigger numbers
"""
subtypeSpec = ValueRangeConstraint(0x1, 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141)
class ANSBigIntegerList(univ.SequenceOf):
"""
A subytpe of Sequenceof for variable length
list of BigIntegers
"""
componentType = ASNBigInteger()
subtypeSpec = ValueSizeConstraint(1, 9223372036854775807)
class ASNMLSAGSignature(Sequence):
"""
ASN.1 type specification for MLSAG
Ring Signature
"""
componentType = NamedTypes(
NamedType('Ix', ASNBigInteger()),
NamedType('Iy', ASNBigInteger()),
NamedType('c0', ASNBigInteger()),
NamedType('sl', ANSBigIntegerList())
)
尝试使用这种类型:
def get_asn1_encoded(self) -> str:
"""
Get the ring signature as der encoded
:return: asn encoded signature
"""
asn = ASNMLSAGSignature()
asn["Ix"] = self.I().x()
asn["Iy"] = self.I().y()
asn["c0"] = self.c0()
asn["sl"] = self.sl()
serialized = encode(asn)
return serialized.hex()
(注意self.sl()返回整数列表) 在我设置sl值的行上,出现以下错误:
KeyError: PyAsn1Error('NamedTypes can cast only scalar values',)
我是否需要以其他方式将python列表转换为ASN.1列表,或者我的类型定义有问题?
答案 0 :(得分:0)
设法通过将列表类型更改为:(消除了不必要的约束)来解决该问题
class ANSIntegerList(univ.SequenceOf):
"""
A subytpe of Sequenceof for variable length
list of BigIntegers
"""
componentType = ASNBigInteger()
和编码部分将用于:(创建列表对象并使用扩展功能添加值)
def get_asn1_encoded(self) -> str:
"""
Get the ring signature as der encoded
:return: asn encoded signature
"""
asn = ASNMLSAGSignature()
asn["Ix"] = self.I().x()
asn["Iy"] = self.I().y()
asn["c0"] = self.c0()
asnlist = ANSIntegerList()
asnlist.extend(self.sl())
asn["sl"] = asnlist
serialized = encode(asn)
return serialized.hex()