0

$USD

$USD
TUTORIALS ESP32ESP8266MicroPython

ESP32 MicroPython Tutorial: Using SHA-256

DFRobot Jul 13 2017 1589

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

 

Introduction

The objective of this MicroPython Tutorial 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 sha256.py.

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 sha256.py 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:

1   ampy --port COM5 put sha256.py

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:

1  import os

2  os.listdir()

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.
 

Figure 1 – Sha256.py 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.

1  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.

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

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

1   hash = hashObject.hexdigest()
2   print(hash)

You should get an output similar to figure 2.
 

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.

 

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.
 

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


 

NOTE: This article is written by Nuno Santos who is an kindly Electronics and Computers Engineer. live in Lisbon, Portugal. you could check the original article here.

He had written many useful tutorials and projects about ESP32, ESP8266, If you are interested, you could check his blog to know more.

DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.