【问题标题】:How to write ASN1 encoding in python using PyASN INTEGER (1..MAX)?如何使用 PyASN INTEGER (1..MAX) 在 python 中编写 ASN1 编码?
【发布时间】:2014-03-19 20:40:46
【问题描述】:

我正在尝试使用 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) 。我错过了什么?在这种情况下它取的 MAX 值是多少?

【问题讨论】:

    标签: python password-encryption asn.1 pbkdf2


    【解决方案1】:

    您需要像这样将value range constraint 添加到 Integer 对象:

    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 时,都会引发异常。

    【讨论】:

      猜你喜欢
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 2021-11-27
      相关资源
      最近更新 更多