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