Python 对象标识符转换ASN1
我试图在Python中实现ASN1的自定义对象标识符,这些是示例测试用例Python 对象标识符转换ASN1,python,encoding,asn.1,Python,Encoding,Asn.1,我试图在Python中实现ASN1的自定义对象标识符,这些是示例测试用例 asn1_objectidentifier([1,2]) asn1_objectidentifier([1,2,840]) asn1_objectidentifier([1,2,840,5,1000000]) asn1_objectidentifier([1,2,840,5,127,128,129]) 我无法做到的是编码值3。。。以128为基数的值n。 这是到目前为止我的代码 def asn1_objectidentif
asn1_objectidentifier([1,2])
asn1_objectidentifier([1,2,840])
asn1_objectidentifier([1,2,840,5,1000000])
asn1_objectidentifier([1,2,840,5,127,128,129])
我无法做到的是编码值3。。。以128为基数的值n。
这是到目前为止我的代码
def asn1_objectidentifier(oid):
first_octet = 40 * oid[0] + oid[1]
rest = ""
length = 1
for v in range(2, len(oid)):
if oid[v] < 127:
rest += chr(oid[v])
length += 1
else:
rem = oid[v] / 128
first = (rem * 128) ^ 1
second = (oid[v] - first) * 128 ^ 0
rest += int_to_bytestring(first)
rest += int_to_bytestring(second)
length += 2
rest = chr(first_octet) + rest
return chr(0x06) + chr(length) + rest
def asn1\u对象标识符(oid):
第一个八位组=40*oid[0]+oid[1]
rest=“”
长度=1
对于范围(2,len(oid))内的v:
如果oid[v]<127:
rest+=chr(oid[v])
长度+=1
其他:
rem=oid[v]/128
第一个=(rem*128)^1
第二个=(oid[v]-第一个)*128^0
rest+=int_to_bytestring(第一个)
rest+=int_to_bytestring(秒)
长度+=2
rest=chr(第一个八位组)+rest
返回chr(0x06)+chr(长度)+剩余
这里有一种对非负整数进行基数128编码的方法:
def base128_encode(n): # int/long to byte string
if n > 0:
arr = []
while n:
n, rem = divmod(n, 128)
arr.append(chr(rem))
return ''.join(reversed(arr))
elif n == 0:
return '\x00'
else:
raise ValueError
print [ord(c) for c in base128_encode(840)] # --> [6, 72]
下面是一种对非负整数进行基数128编码的方法:
def base128_encode(n): # int/long to byte string
if n > 0:
arr = []
while n:
n, rem = divmod(n, 128)
arr.append(chr(rem))
return ''.join(reversed(arr))
elif n == 0:
return '\x00'
else:
raise ValueError
print [ord(c) for c in base128_encode(840)] # --> [6, 72]
这篇优秀的文章详细地描述了这一点,并且——最有价值的是——给出了许多好的测试用例和边缘用例
顺便说一下:该文档在web上似乎没有一个规范的主页。以上是CiteSerx参考信息的链接;存在其他PDF文件;而且。这篇优秀的文章详细描述了这一点,并且——最有价值的是——给出了许多好的测试用例和边缘用例
顺便说一下:该文档在web上似乎没有一个规范的主页。以上是CiteSerx参考信息的链接;存在其他PDF文件;还有。对于我们这些不熟悉ASN.1的人来说,什么是base 128编码?@martineau我自己也找不到,我找到了一个例子。这是针对1.2.840对象标识符的,840=6*128^1=769 72*128^0=71这篇文章解释了如何对值为128或更大的弧进行编码和解码:还有一个pyasn1实现,它用纯Python进行OID编码。对于我们这些不熟悉ASN.1的人来说,什么是基128编码?@martineau我自己也找不到,我发现了一个例子。这是针对1.2.840对象标识符的,840=6*128^1=769 72*128^0=71本文介绍了如何对值为128或更大的圆弧进行编码和解码:还有一个pyasn1实现,它在纯Python中进行OID编码。