ESP32 MicroPython: Using SHA-256

The objective of this post is to explain how to install a module to use the SHA-256 algorithm with MicroPython, running on the ESP32.


The objective of this post is to explain how to install a module to use the SHA-256 algorithm with MicroPython, running on the ESP32.

We are going to use a module from hashlib which implements the SHA-256 hash algorithm. You can read more about SHA here.


Installing the library

At the time of writing, this was a library that was not included by default on MicroPython’s binary distribution. So, we need to manually install it. Note however that in this case installing means copying the module to our file system and import it from there to use the functions on the command line environment.

So, first of all, we will need copy the module on our MicroPython file system. You can get the source code of the SHA-256 library here. The easiest way is go to the raw view of GitHub, copy the whole code and save it on your computer on a file called

Just to confirm that there are no other module dependencies, you can try to do a control+F on the file to search for the import keyword. None should be found.

Now, we will deal with the upload of the file to the file system. To do so, we will need a Python tool called ampy. You can check this previous tutorial for a detailed explanation on how to install it and use it to upload files to the file system of MicroPython.

So, to proceed with the upload, open a command line and navigate to the directory where you previously saved the file, with the ESP32 board connected to your computer. There, just hit the following command, changing COM5 with the serial port where your board is connected:

ampy --port COM5 put

The file should now be uploaded to your board’s file system. It may take a while because of the size of the file.

Testing the library

To test the library, just connect to the Python prompt using a program like Putty. Once connected, we will confirm that the file is on our file system by sending the following commands:

import os

As can be seen in figure 1, the library is now on the file system. In my case, I have other files from other projects.

ESP32 ESP8266 SHA 256 lib install MicroPython

Figure 1 – module on MicroPython’s file system.

Now, we will test the functionalities of the module. First of all, we will need to import it with the command shown bellow.

import sha256 

Then, we will create an object of class sha256, passing as input the string with the content that we want to hash. We will use a simple test string, as shown in the code bellow.

testString = "String to hash with sha256"
hashObject = sha256.sha256(testString)

Finally, we call the hexdigest method to obtain the hash of our string. This method receives no arguments.

hash = hashObject.hexdigest()

You should get an output similar to figure 2.

ESP32 ESP8266 Sha256 hash

Figure 2 – Result of the program.

We can use this website to confirm if the result of our hash program matches the expected result of applying the SHA-256 algorithm to the string. Figure 3 illustrates the result for the string we used in the Python code.

Result of the hash using sha256

Figure 3 – Applying the SHA-256 algorithm to the string defined in the MicroPyton code.

We can confirm this result in MicroPython by copying the content from the validation website and pasting it to a string to compare with our hash string, as shown in figure 4. It should return “True”, indicating that the content of both matches.

ESP32 ESP8266 MicroPython sha256 algorithm

Figure 4 – Matching comparison between the hash created on MicroPython and on the validation website.


3 Replies to “ESP32 MicroPython: Using SHA-256”

Leave a Reply