how to convert string to uint8_t for Hmac secretkey

Hi, I'm very new in Arduino but giving this kind of project by my supervisor, i need you guys help on my problem.

i take an example from the Sha256.h to show my Hmac problem.

Since Hmac required a secret key to encrypt the message, i allow user to key in their desired secret key by their own through bluetooth. i received as String and now i having problem on converting the String to uint8_t that required by the Sha256.initHmac(const uint_t* secret, int secretLength) below is the code that extract from the example code.

#include "sha256.h"

void printHash(uint8_t* hash) {
  int i;
  for (i=0; i<32; i++) {
    Serial.print("0123456789abcdef"[hash[i]>>4]);
    Serial.print("0123456789abcdef"[hash[i]&0xf]);
  }
  Serial.println();
}

void setup() {
  uint8_t* hash;
  uint32_t a;
  
  Serial.begin(9600);

  Serial.println("Test: RFC4231 4.3");
  Serial.println("Expect:5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");
  Serial.print("Result:");
  Sha256.initHmac((uint8_t *)"Jefe", 4);
  Sha256.print("what do ya want for nothing?");
  printHash(Sha256.resultHmac());
  Serial.println();
}

i had tried some ways that suggested by others on forum that using atoi or charArray or getBytes. but both gives me the wrong result as expected. below is my code that edited.

#include "sha256.h"

void printHash(uint8_t* hash) {
  int i;
  for (i=0; i<32; i++) {
    Serial.print("0123456789abcdef"[hash[i]>>4]);
    Serial.print("0123456789abcdef"[hash[i]&0xf]);
  }
  Serial.println();
}

void setup() {
  uint8_t* hash;
  uint32_t a;
  String input="Jefe";
  char data[4];

  Serial.begin(9600);

  Serial.println("Test: RFC4231 4.3");
  Serial.println("Expect:5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");
  Serial.print("Result:");
  input.toCharArray(data,4);
  Sha256.initHmac((uint8_t *)data, 4);
  Sha256.print("what do ya want for nothing?");
  printHash(Sha256.resultHmac());
  Serial.println();
}

i did tried some other ways for example:

#include "sha256.h"

void printHash(uint8_t* hash) {
  int i;
  for (i=0; i<32; i++) {
    Serial.print("0123456789abcdef"[hash[i]>>4]);
    Serial.print("0123456789abcdef"[hash[i]&0xf]);
  }
  Serial.println();
}

void setup() {
  uint8_t* hash;
  uint32_t a;
  String input="Jefe";
  byte data[4];

  Serial.begin(9600);

  Serial.println("Test: RFC4231 4.3");
  Serial.println("Expect:5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");
  Serial.print("Result:");
  input.getBytes(data,4);
  Sha256.initHmac((uint8_t *)data, 4);
  Sha256.print("what do ya want for nothing?");
  printHash(Sha256.resultHmac());
  Serial.println();
}

Thanks in advance for those who help.

i received as String

That was dumb.

The 2nd argument to toCharArray() is the size of the array to write to. Since toCharArray() is going to NULL terminate the array, the actual number of characters written to the array will be one less than that argument.

Your array to write to is too small.