我试图使用pyasn1对RSA签名进行编码,但是我在SEQUENCE中的默认类型有问题。我定义了两种AlgorithmIdentifier类型,一种是默认的“algorithm”组件,另一种是在新创建的对象上手动设置的。如果我实例化每个对象中的一个,然后调用prettyPrint(),那么输出看起来是相同的。但是,如果我编码错误(或者按顺序编码),默认类型的对象似乎缺少算法组件。在
例如:from pyasn1.type import univ, namedval, namedtype, tag
from pyasn1.codec.ber import encoder
pkcs1 = univ.ObjectIdentifier('1.2.840.113549.1.1')
md5WithRSAEncryption_id = pkcs1 + univ.ObjectIdentifier((4,))
class AlgorithmIdentifier(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.NamedType('algorithm', univ.ObjectIdentifier()),
namedtype.OptionalNamedType('params', univ.Any()))
class AlgorithmIdentifier2(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.DefaultedNamedType('algorithm', md5WithRSAEncryption_id),
namedtype.OptionalNamedType('params', univ.Any()))
a = AlgorithmIdentifier()
a.setComponentByName('algorithm', md5WithRSAEncryption_id)
print a.prettyPrint()
print "Encoded: %s" % encoder.encode(a).encode('hex')
a2 = AlgorithmIdentifier2()
a2.setDefaultComponents()
print a2.prettyPrint()
print "Encoded: %s" % encoder.encode(a2).encode('hex')
输出:
^{pr2}$
我做错什么了?在