2

I want to use SHA1 function from openssl library for hashing a string, I have downloaded the library and installed it in /usr/include, and here is my code:

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

int main() {

    unsigned char digest[SHA_DIGEST_LENGTH];
    char string[] = "hello world";

    SHA1((unsigned char*) &string, strlen(string), (unsigned char*) &digest);
}

It doesn't have any syntax error and it recognizes openssl/sha.h, but when I want to build the project in eclipse or build from the terminal, I get this error:

Hash.cpp:(.text+0x4a): undefined reference to `SHA1'
collect2: error: ld returned 1 exit status

Any help would be appreciated! :)

zakinster
  • 10,508
  • 1
  • 41
  • 52
user1995098
  • 183
  • 1
  • 10
  • So you need to link against the library. What is the command you are using to build from the terminal? – BoBTFish May 16 '13 at 08:15

1 Answers1

5

You didn't properly link openssl, if you're on Linux, you should link crypto.

From a terminal :

g++ -o hash hash.cpp -lcrypto

From eclipse, you should open project->Properties, go to C/C++ Build->Settings and add crypto in the Linker->Libraries folder.

zakinster
  • 10,508
  • 1
  • 41
  • 52