Using SHA1 to check data integrity

To test the integrity of data files often cryptographic Hash algorithms are used. NodeMCU offers several hash algorithms:

  • MD5
  • SHA1
  • MD2 (not available by default, has to be explicitly enabled in user_config.h)
  • SHA256, SHA384, SHA512 (unless disabled in user_config.h)

Default available are the common used algorithms MD5 (Message-Digest Algorithm 5) and SHA1 (Secure Hash Algorithm 1). Both algorithms compute a 128-bit respectively 160-bit hash value (check sum) from arbitrary files.
A common use of these algorithms can be found during file download from a file server over the Internet. The checksum of the original file is provided on the server, and after downloading, the concerned checksum will be calculated again and compared. If the checksums are identical, then the down loaded file is intact and the download was successful.

The program sample crypto.lua calculates the SHA1 checksum from the file credentials.lua which contains sensitive data as user passwords etc. This checksum and the corresponding filename will be saved in a file credentials.sha1, which can be used to compare calculated and saved checksums. The screenshot shows the call of the program sample and its output.

crypto

The program sample crypto.lua is available for download at sourceforge.net.

 

Using SHA1 to check data integrity