Hello,
my program involves hashing, and the hashing library takes strings as inputs.
However because I need to store many hash values in memory (more than 20, 256-bits each) and these are dynamically changed, I cannot fit them into memory so I thought I could change them into byte arrays. However this involves storing the initial values in flash memory and convert them into Byte arrays, then convert them back to strings so I can hash them.
The problem is with the following snippet, I have an array_to_string() function which works for one bytearray, but if I add a second then the program misbehaves (doesn't output anything).
Anyone has an idea of what could be wrong?
const byte MaxByteArraySize = 32;
void hexCharacterStringToBytes(byte *byteArray, const char *hexString)
{
bool oddLength = strlen(hexString) & 1;
byte currentByte = 0;
byte byteIndex = 0;
for (byte charIndex = 0; charIndex < strlen(hexString); charIndex++)
{
bool oddCharIndex = charIndex & 1;
if (oddLength)
{
// If the length is odd
if (oddCharIndex)
{
// odd characters go in high nibble
currentByte = nibble(hexString[charIndex]) << 4;
}
else
{
// Even characters go into low nibble
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
else
{
// If the length is even
if (!oddCharIndex)
{
// Odd characters go into the high nibble
currentByte = nibble(hexString[charIndex]) << 4;
}
else
{
// Odd characters go into low nibble
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
}
}
byte nibble(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 0; // Not a valid hexadecimal character
}
String array_to_string(byte array[], unsigned int len)
{
char buffer[32];
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
//buffer[len*2] = '\0';
return (String(buffer));
}
void setup()
{
Serial.begin(115200);
byte byteArray[MaxByteArraySize] = {0};
byte byteArray2[MaxByteArraySize] = {0};
hexCharacterStringToBytes(byteArray, "173af653133d964edfc16cafe0aba33c8f500a07f3ba3f81943916910c257705");
Serial.println(array_to_string(byteArray,32));
hexCharacterStringToBytes(byteArray2, "3a397279c72e019a31729d7536dfd407c37cc63dcde44a0222be2e098d85181a"); //
Serial.println(array_to_string(byteArray2,32)); //if I comment these 2 lines out then the output for the first byteArray works..
}
void loop() {}
What do you mean by "strings"? If you mean String objects, these are to be avoided on Arduino, because they cause memory problems and program crashes. C-strings are zero terminated character arrays, like "12345".
Please try to clearly describe exactly what you are trying to do. Posting snippets is not useful.
20 x 256 bit hash values require 640 bytes of storage. No problem there.
Here is my complete code. This works fine for up to 6 hash values (SIGMA = 6 in the code). However if I add more the program misbehaves and I also get dynamic memory warnings. Think for 20 values it doesn't work at all.
So my ultimate goal is to minimize memory usage, that's why I thought of converting the hash values to byte arrays...
So my ultimate goal is to minimize memory usage, that's why I thought of converting the hash values to byte arrays...
Each two characters in the string becomes one byte, so you will save the data in half the space.
What IS the problem? Grabbing two characters at a time from a string is trivial. Converting the 2 characters to a byte is simple. Storing the resulting value in an array is a no-brainer.
Ok so yes I have understood that string is "bad" in arduino. However the I am trying to make the hashing function work with char instead of strings but it outputs incorrect values.
Here's a minimal working example of what I mean. h1() which uses string outputs the correct hash of value1 (starts with "6db...") but h2() which uses char outputs wrong hash. Here's the usage example also from the library.
You are confusing the size of a character pointer with the length of a character string. To demonstrate that, I've added a couple of debug prints to the function h2().