I am using crypto-js to calculated the MD5 checksum for my file before uploading, below is my code.
import CryptoJS from "crypto-js";
const getMd5 = async (fileObject) => {
let md5 = "";
try {
const fileObjectUrl = URL.createObjectURL(fileObject);
const blobText = await fetch(fileObjectUrl)
.then((res) => res.blob())
.then((res) => new Response(res).text());
const hash = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(blobText));
md5 = hash.toString(CryptoJS.enc.Hex);
} catch (err) {
console.log("Error occured getMd5:", err);
}
return md5;
};
Above code is working fine for text files only but while working with non text files file images, videos etc., the checksum is calculated incorrectly.
Any help/input is appreciated. Thanks!