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:
- In Python 2, use the defaultText modeOpen,WindowsThe API processes some characters, resulting in inconsistent reading data;
- The correct MD5 value is calculated by PHP.
file_get_contents
Is binary safe;
The correct Python 2 & 3 code is as follows:
hashlib.md5(open("test.torrent", 'rb').read()).hexdigest()
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.