Hello world example of using the hashing functions of mbed TLS. The canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-tls

SHA-256 Hash example on mbed OS

This application performs hashing of a buffer with SHA-256 using various APIs. It serves as a tutorial for the basic hashing APIs of mbed TLS.

Getting started

Building with mbed CLI

If you'd like to use mbed CLI to build this, then you should set up your environment if you have not done so already. For instructions, refer to the main readme. The instructions on this page relate to using the developer.mbed.org Online Compiler

Import the program in to the Online Compiler, select your board from the drop down in the top right hand corner and then compile the application. Once it has built, you can drag and drop the binary onto your device.

Monitoring the application

The output in the terminal window should be similar to this:

terminal output

Method 1: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
Method 2: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
Method 3: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
Method 4: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3

DONE
Revision:
72:6973f9247848
Parent:
60:4ca36ac89018
Child:
75:cc01992ff516
--- a/main.cpp	Mon Oct 08 16:00:15 2018 +0100
+++ b/main.cpp	Mon Oct 08 17:00:15 2018 +0100
@@ -40,7 +40,7 @@
     for (size_t i = 0; i < len; i++)
         mbedtls_printf("%02x", buf[i]);
 
-    mbedtls_printf("\r\n");
+    mbedtls_printf("\n");
 }
 
 static const char hello_str[] = "Hello, world!";
@@ -54,7 +54,7 @@
     // Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information.
     (void)ctx;
 
-    mbedtls_printf("\r\n\r\n");
+    mbedtls_printf("\n\n");
 
     /*
      * Method 1: use all-in-one function of a specific SHA-xxx module
@@ -98,7 +98,7 @@
 
     if (md_info3 == NULL)
     {
-        mbedtls_printf("SHA256 not available\r\n");
+        mbedtls_printf("SHA256 not available\n");
         return 1;
     }
 
@@ -106,7 +106,7 @@
 
     if (ret3 != 0)
     {
-        mbedtls_printf("md() returned -0x%04X\r\n", -ret3);
+        mbedtls_printf("md() returned -0x%04X\n", -ret3);
         return 1;
     }
 
@@ -122,7 +122,7 @@
 
     if (md_info4 == NULL)
     {
-        mbedtls_printf("SHA256 not available\r\n");
+        mbedtls_printf("SHA256 not available\n");
         return 1;
     }
 
@@ -133,7 +133,7 @@
     int ret4 = mbedtls_md_init_ctx(&ctx4, md_info4);
     if (ret4 != 0)
     {
-        mbedtls_printf("md_init_ctx() returned -0x%04X\r\n", -ret4);
+        mbedtls_printf("md_init_ctx() returned -0x%04X\n", -ret4);
         return 1;
     }
 
@@ -151,7 +151,7 @@
     mbedtls_md_free(&ctx4);
 
 
-    mbedtls_printf("\r\nDONE\r\n");
+    mbedtls_printf("\nDONE\n");
 
     return 0;
 }
@@ -161,13 +161,13 @@
     int exit_code = MBEDTLS_EXIT_FAILURE;
 
     if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) {
-        printf("Platform initialization failed with error %d\r\n", exit_code);
+        printf("Platform initialization failed with error %d\n", exit_code);
         return MBEDTLS_EXIT_FAILURE;
     }
 
     exit_code = example(&platform_ctx);
     if (exit_code != 0) {
-        mbedtls_printf("Example failed with error %d\r\n", exit_code);
+        mbedtls_printf("Example failed with error %d\n", exit_code);
         exit_code = MBEDTLS_EXIT_FAILURE;
     }