简体   繁体   English

如果使用中文字符,为什么php md5()与python的hash.md5()总是不同?

[英]Why does php md5() always different from python's hash.md5() if using chinese character?

here is my php code: 这是我的PHP代码:

$str = '你好';
$input_encoding = mb_detect_encoding($str, array('ASCII','GB2312','GBK','UTF-8'), true);
echo sprintf('input encoding:%s', $input_encoding);

$str_gb = iconv($input_encoding, 'GBK', true);
echo sprintf("utf8 encoding:%s\n", $str);
echo sprintf("gb encoding md5:%s\n", md5($str_gb));
echo sprintf("utf8 encoding md5:%s\n", md5($str));

here is my python code: 这是我的python代码:

#!/usr/bin/env python
#coding:utf-8


import urllib
import hashlib

str_u = u'你好'
str_gb = str_u.encode('gbk')
str_u8 = str_u.encode('utf-8')

m = hashlib.md5()
m.update(str_gb)
str_gb_md5 = m.hexdigest()
m.update(str_u8)
str_u8_md5 = m.hexdigest()

print 'gb md5:%s' % str_gb_md5
print 'utf-8 md5:%s' % str_u8_md5

PHP code result is: PHP代码的结果是:

input encoding:CP936
utf8 encoding:你好
gb encoding md5:c4ca4238a0b923820dcc509a6f75849b
utf8 encoding md5:7eca689f0d3389d9dea66ae112e5cfd7

python code result is: python代码的结果是:

gb md5:b94ae3c6d892b29cf48d9bea819b27b9
utf-8 md5:a8a343223373c7d78c3fb8bad2d786c3

And here is my programming environment: PHP 5.5.4 (cli)&Python 2.6.8 这是我的编程环境:PHP 5.5.4(cli)&Python 2.6.8

THX! 谢谢!

There are mistakes in both your php and python code. 您的php和python代码都有错误。 The gbk md5 in you php code and utf8 md5 in you python code are wrong. php代码中的gbk md5和python代码中的utf8 md5是错误的。

Python part: Python部分:

You misunderstand the usage of Python hashlib's hash.update function. 您误解了Python hashlib的hash.update函数的用法。

hash.update(arg) hash.update(ARG)

Update the hash object with the string arg. 使用字符串arg更新哈希对象。 Repeated calls are equivalent to a single call with the concatenation of all the arguments: m.update(a); 重复调用等效于将所有参数串联在一起的单个调用: m.update(a); m.update(b) is equivalent to m.update(a+b) . m.update(b)等同于m.update(a + b)

Fix: 固定:

print  hashlib.md5(str_u8).hexdigest()

7eca689f0d3389d9dea66ae112e5cfd7

PHP part: PHP部分:

You forget to pass $str to the iconv function, instead you passed a true value( which is coverts to 1). 您忘记将$ str传递给iconv函数,而是传递了一个真值(隐蔽性为1)。

Fix: 固定:

$str = '你好';
$str_gb = iconv('UTF-8', 'GBK', $str);
echo sprintf("gb encoding md5:%s\n", md5($str_gb));

output: 输出:

gb encoding md5:b94ae3c6d892b29cf48d9bea819b27b9 gb编码md5:b94ae3c6d892b29cf48d9bea819b27b9

iconv definition : iconv定义

string iconv ( string $in_charset , string $out_charset , string $str )

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

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