# 【无需解决】用Python实现Java中的32位(字节)的md5的计算逻辑即先md5.digest生成16位再合并处理成32位

Python 1913浏览

【已解决】java中md5值的长度是16位(字节)还是32位(字节)及两者之间关系

java中的先用md5.digest生成16位，再用0-9，a-z(或A-Z）去合并计算出32位

python implement java md5.digest 16 to 32
Iterative hashing returns different values in Python and Java – Stack Overflow
Replicate Java’s PBEWithMD5AndDES in Python 2.7 – Stack Overflow
101 – Python Vs Java Md5 Hexdigest – pad.yohdah.com
```//# the PYTHON way
import sys
from hashlib import md5
for arg in sys.argv[1:]:
print md5(arg).hexdigest()
//# The Java way ...
import java.security.MessageDigest;
import java.math.BigInteger;
class MD5 {
public String message;
public MD5( String message ) {
this.message = message;
}
public String hexdigest() throws Exception {
String hd;
MessageDigest md5 = MessageDigest.getInstance( "MD5" );
md5.update( this.message.getBytes() );
BigInteger hash = new BigInteger( 1, md5.digest() );
hd = hash.toString(16); // BigInteger strips leading 0's
while ( hd.length() < 32 ) { hd = "0" + hd; } // pad with leading 0's
return hd;
}
public static void main( String[] args ) throws Exception {
for ( String message: args ) {
System.out.println( new MD5( message ).hexdigest() );
}
}
}```
-》这里是用java实现了16位，然后剩下都是填充0
-》不是我要的
python equivalent of java md5.digest 16
android – Cannot get the same result from a java md5 hash as a python md5 hash – Stack Overflow
Python 2 vs 3. Same inputs, different results. MD5 hash – Stack Overflow
Hashing raw bytes in Python and Java produces different results – Stack Overflow
Java and Python generate different Hmac-SHA256 output – Stack Overflow
python 如何将md5转为16字节 – 翔云 – CSDN博客

-》如果是，那就不用去写代码了，否则生成的结果还是一样的
-》还是解决不了问题啊
-》不过还是继续去试试再说

https://blog.csdn.net/lanyang123456/article/details/79514914

`int_hex = int(s, 16)`

【已解决】java中md5值的长度是16位(字节)还是32位(字节)及两者之间关系

`hexDigits[byte0 >>> 4 & 0xf]`
-》所以其实就是一样的，无需再去写Python了。

```md5Digest = md5Result.digest()
print("md5Digest=%s" % md5Digest) #
print("len(md5Digest)=%s" % len(md5Digest))
md5Hexdigest = md5Result.hexdigest()
print("md5Hexdigest=%s" % md5Hexdigest)
print("len(md5Hexdigest)=%s" % len(md5Hexdigest))```

```md5Digest=b'\xde\xfc;#\x11g\xe1i2\xea\x92=an-@'
len(md5Digest)=16
md5Hexdigest=defc3b231167e16932ea923d616e2d40
len(md5Hexdigest)=32```

python md5 digest vs hexdigest
hash – MD5 digest vs. hexdigest collision risk – Stack Overflow
How to get MD5 sum of a string using python? – Stack Overflow
Use `.hexdigest()` rather than `.digest(‘hex’)`? · Issue #2 · sharedstreets/sharedstreets-python
python3中digest()和hexdigest()区别 – 悠然现南山 – 博客园
【总结】

```  {
char[] arrayOfChar = new char[16];
char[] tmp6_5 = arrayOfChar;
tmp6_5[0] = 48;
char[] tmp12_6 = tmp6_5;
tmp12_6[1] = 49;
char[] tmp18_12 = tmp12_6;
tmp18_12[2] = 50;
char[] tmp24_18 = tmp18_12;
tmp24_18[3] = 51;
char[] tmp30_24 = tmp24_18;
tmp30_24[4] = 52;
char[] tmp36_30 = tmp30_24;
tmp36_30[5] = 53;
char[] tmp42_36 = tmp36_30;
tmp42_36[6] = 54;
char[] tmp49_42 = tmp42_36;
tmp49_42[7] = 55;
char[] tmp56_49 = tmp49_42;
tmp56_49[8] = 56;
char[] tmp63_56 = tmp56_49;
tmp63_56[9] = 57;
char[] tmp70_63 = tmp63_56;
tmp70_63[10] = 97;
char[] tmp77_70 = tmp70_63;
tmp77_70[11] = 98;
char[] tmp84_77 = tmp77_70;
tmp84_77[12] = 99;
char[] tmp91_84 = tmp84_77;
tmp91_84[13] = 100;
char[] tmp98_91 = tmp91_84;
tmp98_91[14] = 101;
char[] tmp105_98 = tmp98_91;
tmp105_98[15] = 102;
tmp105_98;
try
{
paramString = paramString.getBytes();
Object localObject = MessageDigest.getInstance("MD5");
((MessageDigest)localObject).update(paramString);
paramString = ((MessageDigest)localObject).digest();
int i = paramString.length;
localObject = new char[i * 2];
int j = 0;
int k = 0;
while (j < i)
{
int m = paramString[j];
int n = k + 1;
localObject[k] = ((char)arrayOfChar[(m >> 4 & 0xF)]);
k = n + 1;
localObject[n] = ((char)arrayOfChar[(m & 0xF)]);
j++;
}
paramString = new String((char[])localObject);
return paramString;
}
catch (Exception paramString) {}
return null;
}```
md5的计算逻辑，和此处Python的md5计算逻辑：
```md5Hexdigest = md5Result.hexdigest()
print("md5Hexdigest=%s" % md5Hexdigest)```

【已解决】java中md5值的长度是16位(字节)还是32位(字节)及两者之间关系

```md5Digest = md5Result.digest()
print("md5Digest=%s" % md5Digest) #
print("len(md5Digest)=%s" % len(md5Digest))
md5Hexdigest = md5Result.hexdigest()
print("md5Hexdigest=%s" % md5Hexdigest)
print("len(md5Hexdigest)=%s" % len(md5Hexdigest))```

```md5Digest=b'\xde\xfc;#\x11g\xe1i2\xea\x92=an-@'
len(md5Digest)=16
md5Hexdigest=defc3b231167e16932ea923d616e2d40
len(md5Hexdigest)=32```

python 如何将md5转为16字节 – 翔云 – CSDN博客

```import hashlib

def get_md5(s):
m = hashlib.md5(s)
return m.hexdigest()

def convert_md5(origin):
result = []
s = ""
for i in range(len(origin)):
s += origin[i]
if i %2 != 0 :
int_hex = int(s, 16)
result.append(int_hex)
s = ""
return result

if __name__=="__main__":
sum = get_md5("hello world")
print sum
print len(sum)
cv_sum = convert_md5(sum)
print cv_sum
print len(cv_sum)

# 5eb63bbbe01eeed093cb22bb8f5acdc3
# 32
# [94, 182, 59, 187, 224, 30, 238, 208, 147, 203, 34, 187, 143, 90, 205, 195]
# 16```

89 queries in 0.160 seconds, using 22.12MB memory