Merge pull request #24 from balloob/md5-on-demand

Make MD5 on demand
This commit is contained in:
Adwait 2022-07-19 11:52:04 +05:30 committed by GitHub
commit 4ac45361e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

View File

@ -755,7 +755,9 @@ class ESPLoader {
compress=true, compress=true,
/* function(fileIndex, written, total) */ /* function(fileIndex, written, total) */
reportProgress=undefined, reportProgress=undefined,
} = {}) => { /* function(image: string) => string */
calculateMD5Hash=undefined
}) => {
console.log("EspLoader program"); console.log("EspLoader program");
if (flash_size !== 'keep') { if (flash_size !== 'keep') {
let flash_end = this.flash_size_bytes(flash_size); let flash_end = this.flash_size_bytes(flash_size);
@ -782,8 +784,11 @@ class ESPLoader {
continue; continue;
} }
image = this._update_image_flash_params(image, address, flash_size, flash_mode, flash_freq); image = this._update_image_flash_params(image, address, flash_size, flash_mode, flash_freq);
let calcmd5 = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(image)); let calcmd5;
console.log("Image MD5 " + calcmd5); if (calculateMD5Hash) {
calcmd5 = calculateMD5Hash(image);
console.log("Image MD5 " + calcmd5);
}
let uncsize = image.length; let uncsize = image.length;
let blocks; let blocks;
if (compress) { if (compress) {
@ -843,13 +848,15 @@ class ESPLoader {
if (compress) { if (compress) {
this.log("Wrote " + uncsize + " bytes (" + bytes_sent + " compressed) at 0x" + address.toString(16) + " in "+(t/1000)+" seconds."); this.log("Wrote " + uncsize + " bytes (" + bytes_sent + " compressed) at 0x" + address.toString(16) + " in "+(t/1000)+" seconds.");
} }
let res = await this.flash_md5sum(address, uncsize); if (calculateMD5Hash) {
if (new String(res).valueOf() != new String(calcmd5).valueOf()) { let res = await this.flash_md5sum(address, uncsize);
this.log("File md5: " + calcmd5); if (new String(res).valueOf() != new String(calcmd5).valueOf()) {
this.log("Flash md5: " + res); this.log("File md5: " + calcmd5);
throw new ESPError("MD5 of file does not match data in flash!") this.log("Flash md5: " + res);
} else { throw new ESPError("MD5 of file does not match data in flash!")
this.log("Hash of data verified."); } else {
this.log("Hash of data verified.");
}
} }
} }
this.log("Leaving..."); this.log("Leaving...");

View File

@ -312,7 +312,8 @@ programButton.onclick = async () => {
flash_size: 'keep', flash_size: 'keep',
reportProgress(fileIndex, written, total) { reportProgress(fileIndex, written, total) {
progressBars[fileIndex].value = written / total * 100; progressBars[fileIndex].value = written / total * 100;
} },
calculateMD5Hash: (image) => CryptoJS.MD5(CryptoJS.enc.Latin1.parse(image)),
}); });
} catch (e) { } catch (e) {
console.error(e); console.error(e);