-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.cpp
More file actions
33 lines (29 loc) · 840 Bytes
/
crypto.cpp
File metadata and controls
33 lines (29 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "crypto.h"
#include "secrets.h"
#include <time.h>
#include "mbedtls/md.h"
String getNowTime() {
struct tm t;
if (!getLocalTime(&t)) return "0000-00-00 00:00:00";
char buf[20];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &t);
return String(buf);
}
String hmacSign(const String& payload) {
byte hmac[32];
mbedtls_md_context_t ctx;
mbedtls_md_init(&ctx);
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1);
mbedtls_md_hmac_starts(&ctx,
(const unsigned char*)SECRET_KEY, strlen(SECRET_KEY));
mbedtls_md_hmac_update(&ctx,
(const unsigned char*)payload.c_str(), payload.length());
mbedtls_md_hmac_finish(&ctx, hmac);
mbedtls_md_free(&ctx);
String out;
for (int i = 0; i < 32; i++) {
if (hmac[i] < 16) out += "0";
out += String(hmac[i], HEX);
}
return out;
}