Hi all, How to gen 32bytes hex string by openssl, I try to use RAND_byte, which is not in hex format, many thanks!!
Hi all, How to gen 32bytes hex string by openssl, I try to use RAND_byte, which is not in hex format, many thanks!!
What do you mean by "not in hex format"? Do you have some example code and output?
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
for example:
unsigned char key[49];
RAND_bytes(key,48);
printf("Key:%s\n",key);
the outcome like this:
Key:\360\330{\217\226\227.5\340!;\211b\302\353\233 \3640\304\346\337Z>\222\2257\227;\35560\225\262\25 0\334gyyX\340\377\277g#M\220\274\321
That's just a matter of how it's being displayed. Try this:
EDIT:Code:#define KEY_SIZE 48 int i; unsigned char key[KEY_SIZE]; RAND_bytes(key, KEY_SIZE); for (i = 0; i < KEY_SIZE; i++) printf("%02X ", key[i]);
If you actually need a string representation of the hex digits you can use sprintf:
Code:#define KEY_SIZE 48 int i; unsigned char key[KEY_SIZE]; char keystr[KEY_SIZE * 2 + 1]; RAND_bytes(key, KEY_SIZE); for (i = 0; i < KEY_SIZE; i++) sprintf(keystr + 2*i, "%02X", key[i]); printf("%s\n", keystr);
Last edited by oogabooga; 04-05-2012 at 11:51 PM.
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
Oh!!!!! You are great!!! thx a lot!!!!
thx oogabooga, I want to ask 1 more question, Can I change this result to Charater pointer? (Char array (HEX) to Char * (HEX))
An array is implictly treated as a pointer to its first element when you pass it to the function. You don't need to do anything.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"