[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: segmentation fault with libcrypto.so (but not libcrypto.a)



[N N]
> #include <openssl/hmac.h>
> #include <stdio.h>
> 
> int main(int argc, char** argv) {
>   unsigned char foo[10] = "boo";
>   printf("%s\n", SHA1(foo, 3, 0));
> }

Don't use printf("%s") on binary data that is not null-terminated.
What you probably want is to convert each byte to 2 hex digits and
print it that way.  See below.
-- 
Peter Samuelson | org-tld!p12n!peter | http://p12n.org/


#include <openssl/sha.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv) {
    int i;
    char hex[] = "0123456789abcdef";
    unsigned char foo[10] = "boo";
    unsigned char *out = SHA1(foo, 3, 0);

    for (i = 0; i < 20; i++)
        printf("%c%c",
               hex[out[i] >> 4],
               hex[out[i] & 0x0f]);
    putc('\n', stdout);
    exit(0);
}


Reply to: