27 lines
874 B
C++
27 lines
874 B
C++
#include "hash.h"
|
|
#include "mbedtls/md.h" // for SHA256
|
|
|
|
// Logging
|
|
static const char* TAG = "HASH";
|
|
|
|
String get_sha256(String payload) {
|
|
byte shaResult[32];
|
|
mbedtls_md_context_t ctx;
|
|
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
|
|
mbedtls_md_init(&ctx);
|
|
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 0);
|
|
mbedtls_md_starts(&ctx);
|
|
mbedtls_md_update(&ctx, (const unsigned char *) payload.c_str(), payload.length());
|
|
mbedtls_md_finish(&ctx, shaResult);
|
|
mbedtls_md_free(&ctx);
|
|
// convert to hex string
|
|
char buffer[sizeof(shaResult)*2 + 1];
|
|
const char hexmap[] = "0123456789abcdef";
|
|
for (int i = 0; i < sizeof(shaResult); i++) {
|
|
buffer[i*2] = hexmap[(shaResult[i] >> 4) & 0x0F];
|
|
buffer[i*2+1] = hexmap[shaResult[i] & 0x0F];
|
|
}
|
|
buffer[sizeof(buffer) - 1] = '\0';
|
|
return String(buffer);
|
|
}
|