fatal error: openssl/sha.h: No such file or directory 解决方案


原文:http://blog.youkuaiyun.com/xxxxxx91116/article/details/7927520

采集
采集
采集
### 如何使用 OpenSSL 的 `sha.h` 头文件实现 SHA-1 计算 为了在 C/C++ 中利用 OpenSSL 库计算 SHA-1 哈希值,需要确保已安装 OpenSSL 开发库并正确配置编译环境。以下是具体方法以及完整的示例代码。 #### 安装依赖 如果在基于 Debian 或 Ubuntu 的系统上工作,则可以通过以下命令安装必要的开发包: ```bash sudo apt-get install libssl-dev [^1] ``` 对于 Windows 用户,可以参考官方文档获取适用于该平台的构建指南[^2]。 #### 配置项目 当完成上述步骤之后,在自己的工程项目里加入 OpenSSL 提供的相关路径支持。通常情况下这涉及两个方面——一是包含头文件的位置;二是链接静态或者动态共享对象形式存在的函数实现部分。例如: 假设已经通过 Linux 平台下的标准方式部署好了 OpenSSL ,那么只需要简单地告诉编译器去哪里寻找这些资源即可。比如采用 GCC 进行编译的时候可以用 `-I` 参数指定额外查找 include 文件夹下是否存在所需 .h 文件的同时也得加上 `-L` 来表明实际二进制模块所在位置,并且记得附加 `-lcrypto` 请求连接对应的库项[^3]。 #### 示例代码 下面展示了一段简单的程序用来演示如何调用 OpenSSL API 执行字符串到其对应SHA-1散列转换操作的过程: ```cpp #include <iostream> #include <iomanip> #include <string> #include <openssl/sha.h> // Include the header file for SHA functions. std::string calculate_sha1(const std::string& input){ unsigned char hash[SHA_DIGEST_LENGTH]; // Define an array to hold result. SHA_CTX sha_ctx; // Create context structure instance. SHA1_Init(&sha_ctx); // Initialize it properly before use. SHA1_Update(&sha_ctx, input.c_str(), input.size()); // Process message bytes as data. SHA1_Final(hash, &sha_ctx); // Finalize calculation and store results into 'hash'. // Convert raw byte sequence from hashing process above back again but now represented by hexadecimal digits instead. std::ostringstream oss; for(int i=0;i<sizeof(hash)/sizeof(hash[0]);i++) { oss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; } return oss.str(); } int main(){ const auto test_string="hello world"; const auto hashed_result=calculate_sha1(test_string); std::cout<<"The SHA-1 digest of \""<<test_string<<"\" is:\n"<<hashed_result<<"\n"; return 0; } ``` 此代码片段定义了一个名为 `calculate_sha1()` 的辅助功能,接受任意长度的标准模板库(STL)风格字符串作为参数输入端口,随后返回另一个 STL 字符串表示经由 SHA-1 算法处理后的十六进制编码版本摘要信息。 #### 编译指令 要成功编译上面给出的例子,请尝试运行如下所示的一条 shell 脚本语句: ```bash g++ -o sha_example sha_example.cpp -lssl -lcrypto ``` 这里假定源码保存在一个叫做 `sha_example.cpp` 的单独文本档当中,而最终生成可执行文件命名为 `sha_example`. --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值