简体   繁体   English

python中MD5校验和的长度变化

[英]Varying length of MD5 checksum in python

I use the following code to compute the binary representation of MD5 hashcode. 我使用以下代码来计算MD5哈希码的二进制表示形式。

MD5 is always 128 bytes, and bin returns a string starting with "0b". MD5 始终为 128个字节,并且bin返回以“ 0b”开头的字符串。 Therefore, the length of md5_bin must always be 130, but when I run the program, it varies between 128 and 130, on different values of random_str . 因此, md5_bin的长度必须始终为130,但是当我运行该程序时,它在128和130之间变化,并且使用random_str不同值。

md5_bin = bin(int(hashlib.md5(random_str).hexdigest(),16))`
print len(md5_bin)

Sure, MD5 is always 128 bytes, but sometimes the first byte is a 0, and occasionally the second byte is too. 当然,MD5始终为128个字节,但有时第一个字节为0,有时第二个字节也为0。

Think of it this way: the decimal string '15' and the decimal '0015' are both the same number 15 . 这样想:十进制字符串'15'和十进制'0015'都是相同的数字15 When you ask Python to convert the int 15 to a string, you're going to get '15' , not '0015 '. 当您要求Python将int 15转换为字符串时,您将得到'15'而不是'0015 '。 It has no way of knowing that you wanted 4 digits instead of 2: 它无法知道您想要的是4位数字,而不是2位数字:

>>> n = int('0015')
>>> str(n)
'15'

And it's the same with bin . bin It has no way of knowing that you wanted 128 bits instead of 126. You gave it a number with 126 bits, so it gives you 126 binary digits. 它无法知道您想要的是128位而不是126位。您给它提供了一个126位的数字,因此它提供了126个二进制数字。

But you can tell it you want that, eg, with a format spec : 但是您可以告诉它,例如,使用format spec

bits = format(md5_bin, '0128b')

… or, equivalently: …或等价地:

bits = '{:0128b}'.format(md5_bin)

If you want the 0b prefix, you can add that: 如果要使用0b前缀,则可以添加:

bits = format(md5_bin, '#0128b')
bits = '{md5_bin:#0128b}'.format(md5_bin)
bits = '0b{md5_bin:0128b}'.format(md5_bin)

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

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