Change analogWrite to ledcWrite, configuration improvements
This commit is contained in:
32
src/hash.cpp
Normal file
32
src/hash.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "hash.h"
|
||||
#include "mbedtls/md.h" // for SHA256
|
||||
|
||||
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';
|
||||
String hash = String(buffer);
|
||||
|
||||
Serial.print("SHA256 payload: ");
|
||||
Serial.print(payload);
|
||||
Serial.println();
|
||||
Serial.print("SHA256-Hash: ");
|
||||
Serial.print(hash);
|
||||
Serial.println();
|
||||
|
||||
return hash;
|
||||
}
|
||||
Reference in New Issue
Block a user