How to generate checksum using micropython?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
AmirJ
Posts: 26
Joined: Tue Sep 03, 2019 9:48 am

How to generate checksum using micropython?

Post by AmirJ » Tue Sep 03, 2019 11:27 am

How to generate checksum using micropython?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to generate checksum using micropython?

Post by jimmo » Tue Sep 03, 2019 12:48 pm

Which checksum algorithm do you need to use? CRC32?

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

Re: How to generate checksum using micropython?

Post by AmirJ » Wed Sep 04, 2019 5:05 am

With thanks for your attention

I do not want CRC Check.
I don't know the check algorithm. but i have very example:

"String" => Checksum

"ABC"=>85
"CBA"=>85

"A"=>84
"BBB"=>87

"CCC"=>86
"C"=>86

"123"=>37
"321"=>37
"213"=>37
"132"=>37

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to generate checksum using micropython?

Post by pythoncoder » Wed Sep 04, 2019 6:13 am

That is a very poor algorithm. I can't deduce how it works from those examples but maybe someone else can.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How to generate checksum using micropython?

Post by Roberthh » Wed Sep 04, 2019 6:55 am

Yes, that looks like some variant of an LRC. Do you also have an example for two identical characters, like "AA"?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to generate checksum using micropython?

Post by jimmo » Wed Sep 04, 2019 7:00 am

It's

Code: Select all

def checksum(msg):
  v = 21
  for c in msg:
    v ^= ord(c)
  return v

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How to generate checksum using micropython?

Post by Roberthh » Wed Sep 04, 2019 7:08 am

good!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to generate checksum using micropython?

Post by pythoncoder » Thu Sep 05, 2019 8:00 am

So it is! Nice one ;)
Peter Hinch
Index to my micropython libraries.

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

Re: How to generate checksum using micropython?

Post by AmirJ » Fri Sep 06, 2019 3:29 pm

jimmo wrote:
Wed Sep 04, 2019 7:00 am
It's

Code: Select all

def checksum(msg):
  v = 21
  for c in msg:
    v ^= ord(c)
  return v



That's great, thank you
how did you know?
How did you find
Thank you again.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to generate checksum using micropython?

Post by jimmo » 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.

Post Reply