How to generate checksum using micropython?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
Roberthh
Posts: 1572
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How to generate checksum using micropython?

Post by Roberthh » Sat Sep 07, 2019 6:28 am

This is a very good explanation.

User avatar
AmirJ
Posts: 18
Joined: Tue Sep 03, 2019 9:48 am

Re: How to generate checksum using micropython?

Post by AmirJ » Sat Sep 07, 2019 9:27 am

jimmo wrote:
Sat Sep 07, 2019 12:18 am
AmirJ wrote:
Fri Sep 06, 2019 3:29 pm
How did you find
There are three clues:
- Ordering doesn't matter. (e.g. ABC == CBA) so it's likely that the checksum is processed byte at a time.
- Making it longer (especially repeating, and even more so, triple repeats) doesn't change the result dramatically. Not likely to be adding the bytes together, like many other simple checksums. This leaves XOR as the likely candidate. Triple AAA giving the same result as single A is a good confirmation of XOR.
- A, B, C checksums clustered around a different value to 1, 2, 3 based one. Means that it's possibly based on the ascii value (i.e. ord). Also A and C going to values two apart (84 and 86) suggests there's no non-linear change to the byte value before whatever accumulation happens.

XOR-based ones often start with a non-zero value, this is so that checksumming a message of NUL bytes (i.e. all zeros) doesn't also give zero as the result.

At that point it's just algebra to solve for the starting value -- ord('A') ^ 84 --> 21. Check with 21^ord('C') --> 86, and 21^ord('1')^ord('2')^ord('3') --> 37.

Very good
Very clever
Well done

Post Reply