如何使用OpenSSL的SHA256功能 [英] How to use OpenSSL's SHA256 functions

查看:6678
本文介绍了如何使用OpenSSL的SHA256功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来熟悉OpenSSL,libncurses和UDP网络。我决定使用OpenSSL的SHA256来熟悉行业加密标准,但是我在解决它的问题上存在问题。我已经将OpenSSL与编译程序的链接错误隔离了。我正在研究Ubuntu 12.10,64位。我已经安装了libssl-dev软件包。



例如,C ++ main.cpp:

  #include< iostream> 
#include< sstream>
#include< string>
#include< iomanip>
使用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<<十六进制<< (2)<< setfill('0')<< (int)的散列[I];
}
返回ss.str();
}

int main()
{

cout<< sha256(测试)< ENDL;
cout<< sha256(test2)<< ENDL;

返回0;




$ b我使用SHA256()函数找到 here 作为OpenSSL SHA256功能的封装。



当我尝试使用以下g ++参数进行编译时,我收到以下错误:

  millinon @ myhost :〜/ Programming / sha256 $ g ++ -lssl -lcrypto -o main main.cpp 
/tmp/ccYqwPUC.o:函数`sha256(std :: string)':
main.cpp :( (.text + 0x38):未定义对`SHA256_Init'的引用
main.cpp :(。text + 0x71):未定义对`SHA256_Update'的引用
main.cpp :( .text + 0x87):undefined reference到`SHA256_Final'
collect2:error:ld返回1退出状态

可以识别OpenSSL定义的函数和类型,但ld无法找到sha.h中提到的函数符号。



我是否需要手动指向特定的共享对象t或目录?



谢谢!

解决方案

常见的初学者错误......在构建时,将链接的库放在命令行的错误位置。



命令行中的依赖关系相反,取决于别的东西,实际上应该放在之前它依赖的命令行上。



在你的例子中,你有一个源文件 main.cpp 取决于一些库集合,那么源文件应该放在它所依赖的库之前:

  $ g ++ -o main main.cpp -lssl -lcrypto 

为了安全起见,在命令行上列出任何源文件或目标文件之后,总是将库 last 放入。


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.

Take, for instance, the 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 here as a wrapper for OpenSSL's SHA256 functionality.

When I attempt to compile with the following g++ arguments, I receive the following error:

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.

Do I need to manually point to a specific shared object or directory?

Thanks!

解决方案

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 before what 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:

$ 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.

这篇关于如何使用OpenSSL的SHA256功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆