C++ 如何使用 OpenSSL 的 SHA256 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13784434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use OpenSSL's SHA256 functions
提问by millinon
I'm writing a program to get myself acquainted with OpenSSL, libncurses, and UDP networking. I decided to work with OpenSSL's SHA256 to become familiar with industry encryption standards, but I'm having issues with getting it working. I've isolated the error to the linking of OpenSSL with the compiled program. I'm working on Ubuntu 12.10, 64 bit. I have the package libssl-dev installed.
我正在编写一个程序来让自己熟悉 OpenSSL、libncurses 和 UDP 网络。我决定使用 OpenSSL 的 SHA256 来熟悉行业加密标准,但我在使用它时遇到了问题。我已将错误与 OpenSSL 与编译程序的链接隔离开来。我正在 64 位 Ubuntu 12.10 上工作。我安装了 libssl-dev 包。
Take, for instance, the C++ main.cpp:
以 C++ main.cpp 为例:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
#include <openssl/sha.h>
string sha256(const string str)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, str.c_str(), str.size());
SHA256_Final(hash, &sha256);
stringstream ss;
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
return ss.str();
}
int main()
{
cout << sha256("test") << endl;
cout << sha256("test2") << endl;
return 0;
}
I'm using the SHA256() function found hereas a wrapper for OpenSSL's SHA256 functionality.
我正在使用此处找到的 SHA256() 函数作为 OpenSSL 的 SHA256 功能的包装器。
When I attempt to compile with the following g++ arguments, I receive the following error:
当我尝试使用以下 g++ 参数进行编译时,收到以下错误:
millinon@myhost:~/Programming/sha256$ g++ -lssl -lcrypto -o main main.cpp
/tmp/ccYqwPUC.o: In function `sha256(std::string)':
main.cpp:(.text+0x38): undefined reference to `SHA256_Init'
main.cpp:(.text+0x71): undefined reference to `SHA256_Update'
main.cpp:(.text+0x87): undefined reference to `SHA256_Final'
collect2: error: ld returned 1 exit status
So, GCC clearly recognizes OpenSSL's defined functions and types, but ld is failing to find the function symbols referred to in sha.h.
因此,GCC 清楚地识别 OpenSSL 定义的函数和类型,但 ld 未能找到 sha.h 中引用的函数符号。
Do I need to manually point to a specific shared object or directory?
我是否需要手动指向特定的共享对象或目录?
Thanks!
谢谢!
回答by Some programmer dude
You make a very common beginners mistake... Putting the libraries you link with in the wrong place on the command line when you build.
你犯了一个非常常见的初学者错误......在构建时将链接的库放在命令行上的错误位置。
Dependencies are reversed on the command line, so something that depends on something else should actually be put beforewhat it depends on on the command line.
依赖关系在命令行上是相反的,所以依赖于其他东西的东西实际上应该放在它依赖于命令行的东西之前。
In your example, you have a source file main.cpp
that depends on some set of libraries, then the source file should be before the libraries it depend on:
在您的示例中,您有一个main.cpp
依赖于某些库集的源文件,那么源文件应该在它所依赖的库之前:
$ g++ -o main main.cpp -lssl -lcrypto
To be safe, always put libraries last, after any source or object files listed on the command line.
为安全起见,始终将库放在最后,在命令行中列出的任何源文件或目标文件之后。
回答by Marshall Clow
This works fine on my system, but you might try:
这在我的系统上运行良好,但您可以尝试:
extern "C" {
#include <openssl/sha.h>
}
which tells g++ that all the stuff in openssl/sha.h is declared as "C" functions.
它告诉 g++ openssl/sha.h 中的所有内容都被声明为“C”函数。
BTW, how old is your OpenSSL?
顺便说一句,你的 OpenSSL 几岁了?