Home > Backend Development > PHP Tutorial > 同一文件分别用PHP/Python/Javascript读取并计算MD5,结果均不同,请教原因及解决方法

同一文件分别用PHP/Python/Javascript读取并计算MD5,结果均不同,请教原因及解决方法

WBOY
Release: 2016-06-06 20:49:33
Original
1493 people have browsed it

测试代码及结果如下:

PHP:

<code class="lang-php">echo md5(file_get_contents('test.torrent'));
//e699b0e7535cebc1b10de1613d6797fb
</code>
Copy after login
Copy after login

Python:

<code class="lang-python">print hashlib.md5(open("test.torrent").read()).hexdigest()
#7cc752c88ae69677afe59ee7c3300e9d
</code>
Copy after login
Copy after login

Javascript:

<code class="lang-javascript">var dragAndDrop = function(){
    if (!window.File || !window.FileList || !window.FileReader) {
        return false;
    }

    var ignoreDrag = function(e) {
        e.originalEvent.stopPropagation();
        e.originalEvent.preventDefault();
    }
    var drop = function(e) {
        ignoreDrag(e);
        var dt = e.originalEvent.dataTransfer;
        var droppedFiles = dt.files;
        $.each(droppedFiles, function(index, file) {
            decodeFile(file);
        });
    }
    $('body')
    .on('dragenter', ignoreDrag)
    .on('dragover', ignoreDrag)
    .on('drop', drop);
};

var decodeFile = function(file) {
    var fileReader = new FileReader();
    var fileData = fileReader.readAsBinaryString(file);
    fileReader.onload = function(){
        var fileData = this.result;
        console.log(CryptoJS.MD5(fileData));
    }
}
dragAndDrop();
 //393fe44680d69ea00fd0a4a2fb3fa8c5
</code>
Copy after login
Copy after login

回复内容:

测试代码及结果如下:

PHP:

<code class="lang-php">echo md5(file_get_contents('test.torrent'));
//e699b0e7535cebc1b10de1613d6797fb
</code>
Copy after login
Copy after login

Python:

<code class="lang-python">print hashlib.md5(open("test.torrent").read()).hexdigest()
#7cc752c88ae69677afe59ee7c3300e9d
</code>
Copy after login
Copy after login

Javascript:

<code class="lang-javascript">var dragAndDrop = function(){
    if (!window.File || !window.FileList || !window.FileReader) {
        return false;
    }

    var ignoreDrag = function(e) {
        e.originalEvent.stopPropagation();
        e.originalEvent.preventDefault();
    }
    var drop = function(e) {
        ignoreDrag(e);
        var dt = e.originalEvent.dataTransfer;
        var droppedFiles = dt.files;
        $.each(droppedFiles, function(index, file) {
            decodeFile(file);
        });
    }
    $('body')
    .on('dragenter', ignoreDrag)
    .on('dragover', ignoreDrag)
    .on('drop', drop);
};

var decodeFile = function(file) {
    var fileReader = new FileReader();
    var fileData = fileReader.readAsBinaryString(file);
    fileReader.onload = function(){
        var fileData = this.result;
        console.log(CryptoJS.MD5(fileData));
    }
}
dragAndDrop();
 //393fe44680d69ea00fd0a4a2fb3fa8c5
</code>
Copy after login
Copy after login

根据你在问题评论中补充的信息,Python 2 版与 PHP 版的不同很容易理解了:

  1. Python 2 中将文件以默认的文本模式打开,Windows API 对部分字符进行处理,导致读到的数据不一致;
  2. 正确的 MD5 值是 PHP 算出来的那个。file_get_contents 是 binary safe 的;
  3. 正确的 Python 2 & 3 代码如下:

    <code>hashlib.md5(open("test.torrent", 'rb').read()).hexdigest()
    </code>
    Copy after login
  4. CryptoJS 这个东西是第三方的。提问者没有提供,因此无法测试。

我做了一下测试,两种python的方法,一种javascript的方法,加一种是Linux shell命令,得出来的MD5都是一样的。PHP没测试,但我相信也应该一样。 python方法一:

<code class="lang-python">import md5
m = md5.new("xxx\n")
print m.hexdigest()
结果是:6de9439834c9147569741d3c9c9fc010
</code>
Copy after login

python方法二:

<code class="lang-python">import hashlib
h = hashlib.md5("xxx\n")
print h.hexdigest()
结果也是:6de9439834c9147569741d3c9c9fc010
</code>
Copy after login

javascript的MD5,我从这下载的:http://www.myersdaily.org/joseph/javascript/md5.js,第101行有个语法错误,加上var即可。得出的结果跟上面的结果一样。

Linux shell:

<code>md5sum
xxx回车后按ctrl + D
</code>
Copy after login

打印出来的结果同上。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template