@@ -138,6 +138,9 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
138138 DEBUG_UPDATER.printf (" [begin] _size: 0x%08X (%d)\n " , _size, _size);
139139#endif
140140
141+ if (!_verify) {
142+ _md5.begin ();
143+ }
141144 return true ;
142145}
143146
@@ -186,44 +189,19 @@ bool UpdaterClass::end(bool evenIfRemaining){
186189 _reset ();
187190 return false ;
188191 }
189- }
190192
191- _md5.begin ();
192- int binSize = _size;
193- if (_hash) {
193+ int binSize = _size - sigLen - sizeof (uint32_t ) /* The siglen word */ ;
194194 _hash->begin ();
195- binSize -= sigLen + sizeof (uint32_t );
196- }
197195#ifdef DEBUG_UPDATER
198- DEBUG_UPDATER.printf (" [Updater] Adjusted binsize: %d\n " , binSize);
196+ DEBUG_UPDATER.printf (" [Updater] Adjusted binsize: %d\n " , binSize);
199197#endif
200- // Calculate the MD5 and hash using proper size
201- uint8_t buff[128 ];
202- for (int i = 0 ; i < binSize; i += sizeof (buff)) {
203- ESP.flashRead (_startAddress + i, (uint32_t *)buff, sizeof (buff));
204-
205- size_t read = binSize - i;
206- if (read > sizeof (buff)) {
207- read = sizeof (buff);
208- }
209- _md5.add (buff, read);
210- if (_hash) {
198+ // Calculate the MD5 and hash using proper size
199+ uint8_t buff[128 ];
200+ for (int i = 0 ; i < binSize; i += sizeof (buff)) {
201+ ESP.flashRead (_startAddress + i, (uint32_t *)buff, sizeof (buff));
202+ size_t read = std::min ((int )sizeof (buff), binSize - i);
211203 _hash->add (buff, read);
212204 }
213- }
214- _md5.calculate ();
215- if (_target_md5.length ()) {
216- if (_target_md5 != _md5.toString ()){
217- _setError (UPDATE_ERROR_MD5);
218- _reset ();
219- return false ;
220- }
221- #ifdef DEBUG_UPDATER
222- else DEBUG_UPDATER.printf (" MD5 Success: %s\n " , _target_md5.c_str ());
223- #endif
224- }
225-
226- if (_verify && _hash) {
227205 _hash->end ();
228206#ifdef DEBUG_UPDATER
229207 unsigned char *ret = (unsigned char *)_hash->hash ();
@@ -250,6 +228,19 @@ bool UpdaterClass::end(bool evenIfRemaining){
250228 _reset ();
251229 return false ;
252230 }
231+ #ifdef DEBUG_UPDATER
232+ DEBUG_UPDATER.printf (" [Updater] Signature matches\n " );
233+ #endif
234+ } else if (_target_md5.length ()) {
235+ _md5.calculate ();
236+ if (_target_md5 != _md5.toString ()){
237+ _setError (UPDATE_ERROR_MD5);
238+ _reset ();
239+ return false ;
240+ }
241+ #ifdef DEBUG_UPDATER
242+ else DEBUG_UPDATER.printf (" MD5 Success: %s\n " , _target_md5.c_str ());
243+ #endif
253244 }
254245
255246 if (!_verifyEnd ()) {
@@ -329,6 +320,9 @@ bool UpdaterClass::_writeBuffer(){
329320 _setError (UPDATE_ERROR_WRITE);
330321 return false ;
331322 }
323+ if (!_verify) {
324+ _md5.add (_buffer, _bufferLen);
325+ }
332326 _currentAddress += _bufferLen;
333327 _bufferLen = 0 ;
334328 return true ;
0 commit comments