gpt4 book ai didi

python - 如何使用 PyASN INTEGER (1..MAX) 在 python 中编写 ASN1 编码?

转载 作者:太空宇宙 更新时间:2023-11-04 06:05:12 24 4
gpt4 key购买 nike

我正在尝试使用 PyASN 库在 Python 中实现一些编码。我必须定义一个类,我应该在其中实现以下内容:

pbkdf2params ::= SEQUENCE {
salt OCTET STRING,
iterationCount INTEGER (1..MAX),
keyLength INTEGER (1..MAX)
}

而我定义的python类如下:

class pbkdf2params(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.NamedType('salt', univ.OctetString()),
namedtype.NamedType('iterationCount', univ.integer(1,MAX)),
namedtype.NamedType('keyLength', univ.integer(1, MAX))
)

但我不认为这是正确的,尤其是对于 integer(1..MAX) 。我错过了什么?在这种情况下它取的最大值是多少?

最佳答案

您需要添加一个 value range constraint像这样的整数对象:

from pyasn1.type import univ, namedtype, constraint

class Pbkdf2params(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.NamedType('salt', univ.OctetString()),
namedtype.NamedType('iterationCount', univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(1,MAX)),
namedtype.NamedType('keyLength', univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(1, MAX))
)

每当您尝试使用超出范围的值初始化 Integer 时,都会抛出异常。

关于python - 如何使用 PyASN INTEGER (1..MAX) 在 python 中编写 ASN1 编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22517626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com