3
I was seeing some examples function hash of Openssl and I came across the format specifier %02x
. I do not know very well its purpose we codes that I saw. I even understand that the %02x
serves to fill the field with zeros and 2 is the number of zeros that will have in this fill (I think this).
Well, here’s some code I made up where I had to use (I don’t know why I had to use) the %02x
:
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <openssl/sha.h>
void get_string(char *string, size_t max_input_size){
fgets(string, max_input_size, stdin);
unsigned int len=strlen(string);
string[len-1]='\0';
}
int main(void){
char string[50];
unsigned char digest[SHA256_DIGEST_LENGTH];
printf("Write >");
get_string(string, 50);
SHA256((unsigned char*)string, strlen(string), digest);
char stringMD[SHA256_DIGEST_LENGTH*2+1];
for(unsigned int i=0; i<SHA256_DIGEST_LENGTH; i++){
sprintf(&stringMD[i*2], "%02x", (unsigned int)digest[i]);
}
printf("\n\nDigest: %s\n\n", stringMD);
return 0;
}
Enforcement (with the %02x
):
Write >FN P90
Digest: 45603ce52cd32a33e3e60c219d0c18e6bd32af24ac39aa000d84e763d23c3031
Execution (with only the %x
):
Write >FN P90
Digest: 45603ce52cd32a33e3e6c
However, what is the purpose of %02x
?