The same file is read and calculated MD5 with Php/python/javascript, the result is different, ask reason and solve method

Source: Internet
Author: User
The test code and results 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 results 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  

Depending on the information you added in the question comments, Python version 2 differs from the PHP version, which is easy to understand:

    1. Python 2 Opens the file in the default text mode , and theWindows API processes some characters, causing the read data to be inconsistent;
    2. The correct MD5 value is the one that PHP calculates. file_get_contentsis a binary safe;
    3. The correct Python 2 & 3 code is as follows:

      hashlib.md5(open("test.torrent", 'rb').read()).hexdigest()
    4. Cryptojs This thing is a third party. The questioner is not provided and therefore cannot be tested.

I did a bit of testing, two Python methods, a JavaScript approach, and a Linux shell command, and the MD5 are the same. PHP is not tested, but I believe it should be the same. Python method One:

import md5m = md5.new("xxx\n")print m.hexdigest()结果是:6de9439834c9147569741d3c9c9fc010

Python method Two:

import hashlibh = hashlib.md5("xxx\n")print h.hexdigest()结果也是:6de9439834c9147569741d3c9c9fc010

JavaScript MD5, I downloaded from this: http://www.myersdaily.org/joseph/javascript/md5.js, the 101th line has a syntax error, plus var. The result is the same as the result above.

Linux Shell:

md5sumxxx回车后按ctrl + D

The printed result is the same as above.

  • 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.