Hi muyouyuwan,
You are using a %s to print the "Hex filled array" and a strlen operation to detect the length. Keep in mind all string operations will treat a 0 value hex as a NULL terminator and try to interpret the hex values as ASCII. I suspect you are encountering the following:
EXAMPLE of Hash:
12bad3007839... up to sha256 len
|
String operations will see the hex 0 and think it is the end of the string
Here is a simple example program to show this difference:
PROGRAM:
#include <stdio.h>
#include <string.h>
int main(void)
{
int i;
unsigned char x[] = {
0x12, 0x34, 0x45, 0x1b, 0xab, 0x89, 0x99,
0x54, 0x78, 0x00, 0x12, 0x34, 0x45, 0x1b
};
printf("Hash1: %s, length %d\n", x, (int) strlen((const char*)x));
printf("Hash2: ");
for (i = 0; i < (int)sizeof(x); i++)
printf("%02x", x[i]);
printf(", length %d\n", (int)sizeof(x));
return 0;
}
OUTPUT:
Hash1: 4E??Tx, length 9
Hash2: 1234451bab89995478001234451b, length 14
Please try the same thing but iterate over the HEX ARRAY using a for loop and printing each value rather than using string operations on hexidecimal values which are very likely to contain 0's.
SIDE NOTE: (Do not use sizeof() on a pointer as it will return the size of the pointer and not the size of the array)
Let me know your results!
Warm Regards,
Kaleb