最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

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

Python crifan 92浏览 0评论
折腾:
【已解决】java中md5值的长度是16位(字节)还是32位(字节)及两者之间关系
期间,问题转化为:
用Python代码实现
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
https://pad.yohdah.com/101/python-vs-java-md5-hexdigest
//# 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博客
好像是我们要的?
不过突然想起来:
难道python的32位字符,本身已经是用同样逻辑生成的字符了?
-》如果是,那就不用去写代码了,否则生成的结果还是一样的
-》还是解决不了问题啊
-》不过还是继续去试试再说
突然再注意到:
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()区别 – 悠然现南山 – 博客园
【总结】
此处,确认了,java中之前碰到的:
  {
    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)
是一致的,都是:
内部如果调用.digest(),则返回的都是16字节的,二进制byte数据
然后用对应的算法,去处理生成32字节的字符串。
对应的算法,详见:
【已解决】java中md5值的长度是16位(字节)还是32位(字节)及两者之间关系
举例,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
而想要把32字节的str转换为16字节的byte,可以参考:
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
即可。

转载请注明:在路上 » 【无需解决】用Python实现Java中的32位(字节)的md5的计算逻辑即先md5.digest生成16位再合并处理成32位

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
21 queries in 0.107 seconds, using 9.35MB memory