Skip to content

Commit 0ca66e3

Browse files
committed
Setup for HMAC authentication capabilities
@SRGDamia1, general HMAC functions could benefit all dataPublishers, so I am adding it to the dataPublisherBase. Does that make sense? Once I get general HMAC SHA256 tokens to work, I'll then be creating a new publisher for Azure EventHubs. AWS IoT has a similar endpoint, so this could be widely used.
1 parent 7a1c63a commit 0ca66e3

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

library.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@
329329
"authors": ["Sara Damiano", "Anthony Aufdenkampe"],
330330
"frameworks": "arduino",
331331
"platforms": "atmelavr, atmelsam"
332-
}
332+
},
333+
{
334+
"name": "cryptosuite2",
335+
"owner": "envirodiy",
336+
"url": "https://github.com/EnviroDIY/cryptosuite2",
337+
"version": "~0.2.7",
338+
"note": "Arduino/Generic C library for SHA256, SHA1 hashing and SHA256-HMAC, SHA1-HMAC",
339+
"authors": [],
340+
"frameworks": "arduino",
341+
"platforms": "*"
342+
}
333343
]
334344
}

src/dataPublisherBase.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,11 @@ String dataPublisher::parseMQTTState(int state) {
155155
default: return String(state) + ": UNKNOWN";
156156
}
157157
}
158+
159+
160+
String dataPublisher::writeHMACsignature(char* key, char* string_to_sign) {
161+
// Create a HexMap to save 16 bytes of SRAM
162+
const char hexMap[] PROGMEM = "0123456789abcdef"; // This is from the cryptosuite2 example, but there must be a better way.
163+
// Anthony to add more code here
164+
return signature;
165+
}

src/dataPublisherBase.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#undef MS_DEBUGGING_STD
5353
#include "LoggerBase.h"
5454
#include "Client.h"
55+
#include "sha256.h" // `cryptosuite2` library's SHA256 functions
5556

5657
/**
5758
* @brief The dataPublisher class is a virtual class used by other publishers to
@@ -268,6 +269,19 @@ class dataPublisher {
268269
*/
269270
String parseMQTTState(int state);
270271

272+
/**
273+
* @brief Write an HMAC-SHA256 signature -- which is a keyed-hash message
274+
* authentication code (HMAC) created using the SHA-256 cryptographic
275+
* hash algorithm -- for generating tokens for authenticating requests
276+
* using the authorization header.
277+
*
278+
* @param key The shared secret key used to "salt" the hash
279+
* @param string_to_sign The string that gets hashed into a signature token.
280+
* @return **String** The signed HMAC-SHA256 authorization token, or
281+
* signature.
282+
*/
283+
String writeHMACsignature(char* key, char* string_to_sign);
284+
271285

272286
protected:
273287
/**

0 commit comments

Comments
 (0)