DER编码的ASN.1序列化和反序列化
bran的Python项目详细描述
bran为ASN.1提供转码器 序列化和反序列化,以及DER-编码。
目的是为本机python类型提供序列化格式, 例如嵌套的dict,其序列化是明确和稳定的。那个 具有相同内容的两个值序列化为相同的字节字符串。
这样就可以创建散列和mac来验证消息 正直。
注意,not使这个包成为^{tt1}的完整实现$ 规格。这不是目标。目标是有一个稳定的字节 python值的表示;特别是DER的选择是因为 这有帮助。
用法
代码
你只需要编码一些值。在大多数情况下,您需要使用DERTranscoder 上课。
test={'some':{'nested':42,'value':(0,1,False)}}frombranimportDERTranscodertranscoder=DERTranscoder()encoded=transcoder.encode(test)decoded=transcoder.decode(encoded)assertdecoded==test
为了让bran如此简单的使用,我们做了一些假设。这个 其中影响最大的是anycollections.Mappingwill be encoded 对于相同的字节表示,这意味着解码后,它将成为 Pythondict。对^{tt6}也作了类似的假设$ 和collections.Sequence。
为了进行哈希运算,请考虑以下代码:
frombran.hashimporthashertest={'some':{'nested':42,'value':(0,1,False)}}h=hasher()h.update(test)print(h.hexdigest())# yields SHA512 hash of the DER serialized testimporthashlibh=hasher(hashfunc=hashlib.md5)h.update(test)print(h.hexdigest())# yields MD5 hash of the DER serialized test
贡献
有关详细信息,请参见CONTRIBUTING.md。
许可证
根据mitnfa(mit+no false attribs)许可证授权。见 LICENSE.txt文件获取详细信息。