简体   繁体   English

忽略前导0的Python编码

[英]Python Encoding that ignores leading 0s

I'm writing code in python 3.5 that uses hashlib to spit out MD5 encryption for each packet once it is is given a pcap file and the password. 我在python 3.5中编写代码,一旦给出了pcap文件和密码,它就会使用hashlib为每个数据包吐出MD5加密。 I am traversing through the pcap file using pyshark. 我正在使用pyshark遍历pcap文件。 Currently, the values it is spitting out are not the same as the MD5 encryptions on the packets in the pcap file. 目前,它吐出的值与pcap文件中数据包的MD5加密不同。

One of the reasons I have attributed this to is that in the hex representation of the packet, the values are represented with leading 0s. 我将此归因于其中一个原因是在数据包的十六进制表示中,值用前导0表示。 Eg: Protocol number is shown as b'06'. 例如:协议号显示为b'06'。 But the value I am updating the hashlib variable with is b'6'. 但我更新hashlib变量的值是b'6'。 And these two values are not the same for same reason: 由于同样的原因,这两个值并不相同:

>> b'06'==b'6'
False

The way I am encoding integers is: 我编码整数的方式是:

(hex(int(value))[2:]).encode()

I am doing this encoding because otherwise it would result in this error: "TypeError: Unicode-objects must be encoded before hashing" 我正在进行此编码,否则会导致此错误:“TypeError:必须在散列之前对Unicode对象进行编码”

I was wondering if I could get some help finding a python encoding library that ignores leading 0s or if there was any way to get the inbuilt hex method to ignore the leading 0s. 我想知道我是否可以找到一些帮助,找到一个忽略前导0的python编码库,或者是否有任何方法可以让内置的十六进制方法忽略前导0。

Thanks! 谢谢!

Hashing b'06' and b'6' gives different results because, in this context, '06' and '6' are different. 散列b'06'b'6'给出不同的结果,因为在这种情况下, b'6'和'6'是不同的。

The b string prefix in Python tells the Python interpreter to convert each character in the string into a byte. Python中的b字符串前缀告诉Python解释器将字符串中的每个字符转换为字节。 Thus, b'06' will be converted into the two bytes 0x30 0x36 , whereas b'6' will be converted into the single byte 0x36 . 因此, b'06'将被转换为两个字节0x30 0x36 ,而b'6'将被转换为单个字节0x36 Just as hashing b'a' and b' a' (note the space) produces different results, hashing b'06' and b'6' will similarly produce different results. 正如散列b'a'b' a' (注意空间)产生不同的结果,散列b'06'b'6'将类似地产生不同的结果。


If you don't understand why this happens, I recommend looking up how bytes work, both within Python and more generally - Python's handling of bytes has always been a bit counterintuitive, so don't worry if it seems confusing! 如果您不明白为什么会发生这种情况,我建议您查看字节是如何工作的,无论是在Python中还是更普遍的情况下 - Python对字节的处理总是有点违反直觉,所以如果看起来有点混乱,请不要担心! It's also important to note that the way Python represents bytes has changed between Python 2 and Python 3, so be sure to check which version of Python any information you find is talking about. 同样重要的是要注意Python表示字节的方式在Python 2和Python 3之间已经发生了变化,因此请务必检查您找到的任何信息所涉及的Python版本。 You can comment here, too, 你也可以在这里评论,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM