Question

I'm heaving a problem on printing a hash generated with OpenSSL, code (using OpenSSL):

char *computeHash(char *msg){
    static char hs[20];

    SHA1(msg, strlen(msg), hs);
    return hs;
}

int main(){
    char *text;
    char *hash;
    int i;

    text = "test";
    hash = computeHash(text);

    for(i=0;i<20;i++){
        printf("%02x",hash[i]);
    }

    return 0;
}

As returning I'm getting:

$ ./a.out ffffffa94affffff8fffffffe5ffffffccffffffb1ffffff9bffffffa61c4c0873ffffffd3ffffff91ffffffe9ffffff87ffffff982fffffffbbffffffd3

Is that any way to print it right?

Thanks,

Was it helpful?

Solution

The %02x format string is for an integer. But you are printing a character. Also, hash is a char * pointer, you probably want an unsigned char *. How about:

unsigned char *hash_ptr = (unsigned char *) hash;
for(i=0;i<20;i++){
    printf("%02x", (int) hash_ptr[i]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top