我正在尝试使用 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 时,都会抛出异常。
我是一名优秀的程序员,十分优秀!