-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
R&D: WAMP CRA #2
Comments
Arduino WAMPCRA SigningCreate the WAMPCRAAuthenticator ClassWAMPCRAAuthenticator.h
WAMPCRAAuthenticator.cpp#include "WAMPCRAAuthenticator.h"
// Constructor to initialize the secret
WAMPCRAAuthenticator::WAMPCRAAuthenticator(const String& secret) {
this->secret = secret;
}
// Function to compute HMAC-SHA256
String WAMPCRAAuthenticator::signChallenge(const String& challenge) {
const size_t key_length = secret.length();
const size_t challenge_length = challenge.length();
unsigned char hmac_result[32]; // HMAC-SHA256 generates a 32-byte output
mbedtls_md_context_t ctx;
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
// Initialize the context for HMAC
mbedtls_md_init(&ctx);
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 1);
mbedtls_md_hmac_starts(&ctx, (const unsigned char*)secret.c_str(), key_length);
mbedtls_md_hmac_update(&ctx, (const unsigned char*)challenge.c_str(), challenge_length);
mbedtls_md_hmac_finish(&ctx, hmac_result);
mbedtls_md_free(&ctx);
// Convert the result into a hex string
String hmac_hex_str = "";
for (int i = 0; i < 32; i++) {
char hex[3];
sprintf(hex, "%02x", hmac_result[i]);
hmac_hex_str += hex;
}
return hmac_hex_str; // Return the hex string
}
Example UsageWAMPCRAAuthenticator.ino
Usage InstructionsCreate the Class Files:Create two files in your Arduino project: WAMPCRAAuthenticator.h and WAMPCRAAuthenticator.cpp. In your main sketch (WAMPCRAAuthenticator.ino), include the class by adding #include "WAMPCRAAuthenticator.h". Upload the Sketch:Upload the sketch to your Arduino board, and it will compute and print the signed challenge in hexadecimal format. |
The code works as expected. I have tested it using |
Need code to sign cra challenge
The text was updated successfully, but these errors were encountered: