C++中的哈希函数SHA1

use*_*098 2 c++ ubuntu openssl sha1

我想使用SHA1的功能从OpenSSL的库散列字符串,我已经下载了该库并安装它/usr/include,这里是我的代码:

#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);
}
Run Code Online (Sandbox Code Playgroud)

它没有任何语法错误,它识别openssl/sha.h,但是当我想在eclipse中构建项目或从终端构建时,我收到此错误:

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

任何帮助,将不胜感激!:)

zak*_*ter 5

你没有正确链接openssl,如果你在Linux上,你应该链接crypto.

从终端:

g++ -o hash hash.cpp -lcrypto
Run Code Online (Sandbox Code Playgroud)

在Eclipse中,你应该打开project->Properties,去C/C++ Build->Settings添加crypto了在Linker->Libraries文件夹中.