Skip to content

Commit c080232

Browse files
committed
Fixed: Compiler error on libb64/cencode.h
1 parent 193cf58 commit c080232

6 files changed

+87
-69
lines changed

changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## Version 2.9.4
3+
- Fixed: Compiler error on libb64/cencode.h when ESP8266WebServer is included before SinricPro.h.
4+
25
## Version 2.9.3
36
- RangeController accepts callbacks for integer and floating point values
47
- Event limitation takes into account different instances for generic controllers

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"maintainer": true
1414
}
1515
],
16-
"version": "2.9.3",
16+
"version": "2.9.4",
1717
"frameworks": "arduino",
1818
"platforms": [
1919
"espressif8266",

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SinricPro
2-
version=2.9.3
2+
version=2.9.4
33
author=Boris Jaeger <[email protected]>
44
maintainer=Boris Jaeger <[email protected]>
55
sentence=Library for https://sinric.pro - simple way to connect your device to alexa

src/SinricProConfig.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// Version Configuration
2626
#define SINRICPRO_VERSION_MAJOR 2
2727
#define SINRICPRO_VERSION_MINOR 9
28-
#define SINRICPRO_VERSION_REVISION 3
28+
#define SINRICPRO_VERSION_REVISION 4
2929
#define SINRICPRO_VERSION STR(SINRICPRO_VERSION_MAJOR) "." STR(SINRICPRO_VERSION_MINOR) "." STR(SINRICPRO_VERSION_REVISION)
3030
#define SINRICPRO_VERSION_STR "SinricPro (v" SINRICPRO_VERSION ")"
3131
#define SINRICPRO_VERISON_INT SINRICPRO_VERSION_MAJOR * 1000000 + SINRICPRO_VERSION_MINOR * 1000 + SINRICPRO_VERSION_REVISION

src/SinricProSignature.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2019 Sinric. All rights reserved.
3+
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
4+
*
5+
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
6+
*/
7+
8+
#include <WString.h>
9+
#include <ArduinoJson.h>
10+
#include "SinricProSignature.h"
11+
12+
#if defined (ESP8266)
13+
#include <bearssl/bearssl_hmac.h>
14+
#endif
15+
#if defined (ESP32)
16+
#include "mbedtls/md.h"
17+
#endif
18+
#include <libb64/cencode.h>
19+
20+
21+
String HMACbase64(const String &message, const String &key) {
22+
byte hmacResult[32];
23+
#if defined(ESP8266)
24+
br_hmac_key_context keyContext; // Holds general HMAC info
25+
br_hmac_context hmacContext; // Holds general HMAC info + specific info for the current operation
26+
27+
br_hmac_key_init(&keyContext, &br_sha256_vtable, key.c_str(), key.length());
28+
br_hmac_init(&hmacContext, &keyContext, 32);
29+
br_hmac_update(&hmacContext, message.c_str(), message.length());
30+
br_hmac_out(&hmacContext, hmacResult);
31+
#endif
32+
33+
#if defined(ESP32)
34+
mbedtls_md_context_t ctx;
35+
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
36+
37+
mbedtls_md_init(&ctx);
38+
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 1);
39+
mbedtls_md_hmac_starts(&ctx, (const unsigned char*) key.c_str(), key.length());
40+
mbedtls_md_hmac_update(&ctx, (const unsigned char*) message.c_str(), message.length());
41+
mbedtls_md_hmac_finish(&ctx, hmacResult);
42+
mbedtls_md_free(&ctx);
43+
44+
#endif
45+
46+
base64_encodestate _state;
47+
base64_init_encodestate(&_state);
48+
#if defined(base64_encode_expected_len_nonewlines)
49+
_state.stepsnewline = -1;
50+
#endif
51+
char base64encodedHMAC[base64_encode_expected_len(32) + 1];
52+
int len = base64_encode_block((const char *)hmacResult, 32, base64encodedHMAC, &_state);
53+
base64_encode_blockend((base64encodedHMAC + len), &_state);
54+
55+
return String { base64encodedHMAC };
56+
}
57+
58+
String extractPayload(const char *message) {
59+
String messageStr(message);
60+
int beginPayload = messageStr.indexOf("\"payload\":");
61+
int endPayload = messageStr.indexOf(",\"signature\"", beginPayload);
62+
if (beginPayload >0 && endPayload >0) return messageStr.substring(beginPayload+10, endPayload);
63+
return "";
64+
}
65+
66+
String calculateSignature(const char* key, String payload) {
67+
if (payload != "") return HMACbase64(payload, String(key));
68+
return "";
69+
}
70+
71+
String signMessage(String key, JsonDocument &jsonMessage) {
72+
if (!jsonMessage.containsKey("signature")) jsonMessage.createNestedObject("signature");
73+
jsonMessage["signature"]["HMAC"] = calculateSignature(key.c_str(), jsonMessage["payload"]);
74+
String signedMessageString;
75+
serializeJson(jsonMessage, signedMessageString);
76+
return signedMessageString;
77+
}

src/SinricProSignature.h

+4-66
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,9 @@
88
#ifndef _SIGNATURE_H_
99
#define _SIGNATURE_H_
1010

11-
#if defined (ESP8266)
12-
#include <bearssl/bearssl_hmac.h>
13-
#endif
14-
#if defined (ESP32)
15-
#include "mbedtls/md.h"
16-
#endif
17-
#include <libb64/cencode.h>
18-
19-
20-
String HMACbase64(const String &message, const String &key) {
21-
byte hmacResult[32];
22-
#if defined(ESP8266)
23-
br_hmac_key_context keyContext; // Holds general HMAC info
24-
br_hmac_context hmacContext; // Holds general HMAC info + specific info for the current operation
25-
26-
br_hmac_key_init(&keyContext, &br_sha256_vtable, key.c_str(), key.length());
27-
br_hmac_init(&hmacContext, &keyContext, 32);
28-
br_hmac_update(&hmacContext, message.c_str(), message.length());
29-
br_hmac_out(&hmacContext, hmacResult);
30-
#endif
31-
32-
#if defined(ESP32)
33-
mbedtls_md_context_t ctx;
34-
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
35-
36-
mbedtls_md_init(&ctx);
37-
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 1);
38-
mbedtls_md_hmac_starts(&ctx, (const unsigned char*) key.c_str(), key.length());
39-
mbedtls_md_hmac_update(&ctx, (const unsigned char*) message.c_str(), message.length());
40-
mbedtls_md_hmac_finish(&ctx, hmacResult);
41-
mbedtls_md_free(&ctx);
42-
43-
#endif
44-
45-
base64_encodestate _state;
46-
base64_init_encodestate(&_state);
47-
#if defined(base64_encode_expected_len_nonewlines)
48-
_state.stepsnewline = -1;
49-
#endif
50-
char base64encodedHMAC[base64_encode_expected_len(32) + 1];
51-
int len = base64_encode_block((const char *)hmacResult, 32, base64encodedHMAC, &_state);
52-
base64_encode_blockend((base64encodedHMAC + len), &_state);
53-
54-
return String { base64encodedHMAC };
55-
}
56-
57-
String extractPayload(const char *message) {
58-
String messageStr(message);
59-
int beginPayload = messageStr.indexOf("\"payload\":");
60-
int endPayload = messageStr.indexOf(",\"signature\"", beginPayload);
61-
if (beginPayload >0 && endPayload >0) return messageStr.substring(beginPayload+10, endPayload);
62-
return "";
63-
}
64-
65-
String calculateSignature(const char* key, String payload) {
66-
if (payload != "") return HMACbase64(payload, String(key));
67-
return "";
68-
}
69-
70-
String signMessage(String key, JsonDocument &jsonMessage) {
71-
if (!jsonMessage.containsKey("signature")) jsonMessage.createNestedObject("signature");
72-
jsonMessage["signature"]["HMAC"] = calculateSignature(key.c_str(), jsonMessage["payload"]);
73-
String signedMessageString;
74-
serializeJson(jsonMessage, signedMessageString);
75-
return signedMessageString;
76-
}
11+
String HMACbase64(const String &message, const String &key);
12+
String extractPayload(const char *message);
13+
String calculateSignature(const char* key, String payload);
14+
String signMessage(String key, JsonDocument &jsonMessage);
7715

7816
#endif // _SIGNATURE_H_

0 commit comments

Comments
 (0)