The same file is read and calculated using PHP, Python, and Javascript respectively. The results are different. Ask the reason and solution.

Source: Internet
Author: User
The test code and result are as follows: PHP: {code...} Python: {code...} Javascript: {code...} The test code and result are as follows:

PHP:

echo md5(file_get_contents('test.torrent'));//e699b0e7535cebc1b10de1613d6797fb

Python:

print hashlib.md5(open("test.torrent").read()).hexdigest()#7cc752c88ae69677afe59ee7c3300e9d

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

Reply content:

The test code and result are as follows:

PHP:

echo md5(file_get_contents('test.torrent'));//e699b0e7535cebc1b10de1613d6797fb

Python:

print hashlib.md5(open("test.torrent").read()).hexdigest()#7cc752c88ae69677afe59ee7c3300e9d

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

Based on the information you added in your comments, it is easy to understand the differences between Python 2 and PHP:

  1. In Python 2, use the defaultText modeOpen,WindowsThe API processes some characters, resulting in inconsistent reading data;
  2. The correct MD5 value is calculated by PHP.file_get_contentsIs binary safe;
  3. The correct Python 2 & 3 code is as follows:

    hashlib.md5(open("test.torrent", 'rb').read()).hexdigest()
  4. CryptoJS is a third party. Unable to test because the questioner does not provide it.

I did a test. There are two python methods, one is javascript, and the other is Linux shell Command. the MD5 obtained is the same. PHP is not tested, but I believe it should be the same. Python Method 1:

Import md5m = md5.new ("xxx \ n") print m. hexdigest (): 6de9439834c9147109741d3c9c9fc010

Python Method 2:

Import hashlibh = hashlib. md5 ("xxx \ n") print h. hexdigest () Result: 6de9439834c9147109741d3c9fc010

MD5 of javascript, which I downloaded from: keystore. The result is the same as the preceding one.

Linux shell:

Md5sumxxx press enter and press ctrl + D

The printed result is the same as the preceding one.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.