Hi,
What I’m trying to achieve is to get an MD5 of a file on a server.
I chose to use fetch to get the file and CryptoJS to calculate the hash but spent already more than 5 hours without luck, I do get a hash but it’s wrong.
To get the right hash I downloaded the file and calculated it manually on linux using md5sum.
First I tried this in NodeJS (although my code should run in a browser) :
fs.readFile('my_file', function(err, buf) {
console.log(md5(buf)); //works but I need CryptoJS not md5
console.log(CryptoJS.MD5(buf.toString("binary")).toString()); //wrong
console.log(CryptoJS.MD5(buf.toString()).toString()); //wrong
});
Later I found a code sample here:
Relevant part is:
function calculateMD5(blob) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.readAsArrayBuffer(blob);
reader.onloadend = function () {
var wordArray = CryptoJS.lib.WordArray.create(reader.result),
hash = CryptoJS.MD5(wordArray).toString();
resolve(hash)
};
})
}
I tested using:
await fetch(URL())
.then((response) => response.blob())
.then(calculateMD5)
But result were wrong.
I have no clue how can I get a right MD5 for a remote file using fetch+CryptoJS