I need to check the md5 of an image in django and in javascript. However I don't obtain the same results. The django code give me the same md5 as md5sum on a terminal.
Django code :
file0 = request.FILES.get('file')
buffer0 = file0.read()
print hashlib.md5(buffer0).hexdigest() //9f982242d24ab97a7254edd7e28e3921
I tried two javascript md5 library, which give me the same results ( https://github.com/blueimp/JavaScript-MD5 and https://github.com/sytelus/CryptoJS ). I have tried different methods (readAsArrayBuffer, readAsBinaryString, ...) and none of them give me the same md5 as python and md5sum.
Javascript :
var reader = new FileReaderSync();
var datas = reader.readAsArrayBuffer(file);
console.log(md5(datas)); //060e4e9e30bcb9ae675a80328a87a687
var string0 = reader.readAsBinaryString(file);
console.log(md5(string0)); //2e4cac0a23ddf95683c6538d64b26e21
console.log(CryptoJS.MD5(string0).toString(CryptoJS.enc.Hex)); //2e4cac0a23ddf95683c6538d64b26e21
var string1 = reader.readAsText(file,'ascii');
console.log(md5(string1)); //329c4271b8eda786213b2468e378b251
console.log(CryptoJS.MD5(string1).toString(CryptoJS.enc.Hex));//329c4271b8eda786213b2468e378b251
var view = new Uint8Array(datas);
var str = ""
for (var i=0, strLen=view.length; i < strLen; i++) {
str+= String.fromCharCode(view[i]);
}
console.log(md5(str)); //2e4cac0a23ddf95683c6538d64b26e21
console.log(CryptoJS.MD5(string1).toString(CryptoJS.enc.Hex));
I think the problem come from how I read the file in JS.