#include "mbedtls/md.h"
#include <string.h>
// Function to compute the hash
void hashSHA256(const char *input, size_t inputLength, unsigned char *outputBuffer) {
mbedtls_md_context_t ctx;
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
mbedtls_md_init(&ctx);
if (mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 0) == 0) {
mbedtls_md_starts(&ctx);
mbedtls_md_update(&ctx, (const unsigned char *)input, inputLength);
mbedtls_md_finish(&ctx, outputBuffer);
}
mbedtls_md_free(&ctx);
}
void setup() {
Serial.begin(115200);
while (!Serial) {}
// Example input
const char input[] = "Hello, SHA-256!";
unsigned char output[32]; // SHA-256 outputs a 32 byte hash
// Compute the hash
hashSHA256(input, strlen(input), output);
// Print the hash in hexadecimal format
Serial.print("SHA-256 hash: ");
for (int i = 0; i < 32; i++) {
char str[3];
sprintf(str, "%02x", output[i]);
Serial.print(str);
}
Serial.println();
}
void loop() {
// No operation
delay(10000);
}