Need assistance translating from Python

Happy Holidays! I am trying to gain access to the charger for an electric vehicle. I found only one reference and it is in Python. It also indicates that the charger uses SHA256 encryption. I am not familiar with either.

After connecting, the charger sends a websocket message with a JSON payload of
{"type":"authRequired","token1":"hrqvxW9Qk5cnYz9AQIHJ7K4uAJQGaQ1b","token2":"GN6iaGAk57F0Rs0iRsAeoYOUm7SS32VR"}

Of course, the tokens change each time. I need to send a response which is calculated in Python as:

1        ran = random.randrange(10**80)
2        self._token3 = "%064x" % ran
3        self._token3 = self._token3[:32]
4        hash1 = hashlib.sha256((
5            message.token1.encode()
6           +self._hashedpassword))
7           .hexdigest()
8        hash = hashlib.sha256((self._token3 +
9            message.token2+hash1).encode()).hexdigest()

My questions:
1: Am I correct that lines 1-3 simply generate 32 random bytes?
2: Line 5 simply ensures that token1 is ASCII (which it already is in my case)?
3: Am I correct that lines 4-6 generates a SHA256 hash of token1 + the hashed password?
4: What does .hexdigest() do? My first assumption was that it was a method of the sha256 library, but research suggests digest() has something to do with concatenation and hexdigest() returns the result of digest() as a hex string?
5: Is customJWT by Ant2000 an appropriate sha256 encoding/decoding library for this?

I'm lost and would greatly appreciate any assistance anyone may be able to provide. Thank you!

i'm not familiar with these functions

  • line 1,2 create an ascii string (token3) with a random value
  • don't know what line 3 does to token3
  • line 4-7 look like they create a sha256 value from the password
  • line 8-9 create a sha256 value from token3

It would probably be faster to use a real computer and Python while you learn about what you can, and cannot accomplish.

Language elements can be translated, but this line of code

hash1 = hashlib.sha256((message.token1.encode() + self._hashedpassword))

calls two methods that you'd have to also dig up and "translate", which methods might use others, or more features of Python that have no direct equivalents in plan C++ coding.

Outside the context of a full Python solution moving it to C++ is not going to be a matter of answering a few questions about one small part.

a7

What mcu are you planning to use...

It would be interesting to see how long a 256 bit sha calculation takes on a 8 bit 16 MHz processor...

Thank you for your comments thus far. Perhaps I didn't make myself perfectly clear.
I have been a professional programmer since 1980 and used every language from COBOL, C in all its variations, Java, more dialects of BASIC, DBase and SQL than I can remember, ABAP, and lots of languages I can't even remember the names of anymore. I never used Python, am now retired and don't really want to learn a new language.
I need to know what those Python expressions do and then I will implement them myself for my project.
I am not running this on an ATMega328 8bit 16MHz MCU, but an ESP32S3, 32bit at 240MHz.
Espressif says that MCU even has a hardware accellerator built-in for SHA256 but I have just started researching that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.