使用 OpenSSL 和 C++ 生成 sha256
我正在寻找使用 openssl 和 C++ 使用 sha256 创建哈希。我知道 Generate SHA hash in C++ using OpenSSL library 有类似的帖子,但我我正在寻找专门创建 sha256 的方法。
更新:
包含路径似乎有问题。 它也找不到任何 OpenSSL 函数
#include "openssl/sha.h"
即使我在构建中包含了路径,
-I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
I'm looking to create a hash with sha256 using openssl and C++. I know there's a similar post at Generate SHA hash in C++ using OpenSSL library, but I'm looking to specifically create sha256.
UPDATE:
Seems to be a problem with the include paths. It can't find any OpenSSL functions even though I included
#include "openssl/sha.h"
and I included the paths in my build
-I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我是这样做的:
它的名字是这样的:
Here's how I did it:
It's called like this:
基于标准
std based
使用 OpenSSL 的 EVP 接口(以下适用于 OpenSSL 1.1)
:这个更高级别接口的优点是,您只需将
EVP_sha256()
调用替换为另一个摘要的函数(例如EVP_sha512()
),即可使用不同的摘要。所以它增加了一些灵活性。Using OpenSSL's EVP interface (the following is for OpenSSL 1.1):
The advantage of this higher level interface is that you simply need to swap out the
EVP_sha256()
call with another digest's function, e.g.EVP_sha512()
, to use a different digest. So it adds some flexibility.随着 OpenSSL 更新到 3.0,大多数解决方案将无法工作(因为 API 已弃用),建议使用
EVP
函数。下面的代码显示了我对 EVP 的看法,基于 OpenSSL 的官方文档此实现使用纯
C
并在Ubuntu 22.04
上进行了验证。这种更高级别接口的优点是您只需更换
EVP_sha256() 调用另一个摘要的函数,例如
EVP_sha512()
,以使用不同的摘要。With the OpenSSL update to 3.0 most of the solution won't work (as the APIs deprecated) and it's recommended to use the
EVP
functions. Below code shows my take with EVP based on OpenSSL's official documentationThis implementation uses pure
C
and verified onUbuntu 22.04
The advantage of this higher level interface is that you simply need to swap out the
EVP_sha256()
call with another digest's function, e.g.EVP_sha512()
, to use a different digest.自 openssl 3.0 起,函数“SHA256_Init”“SHA256_Update”“SHA256_Final”已弃用,这是获取 sha256 的新方法
the functions "SHA256_Init" "SHA256_Update" "SHA256_Final" is deprecated since openssl 3.0, this is the new way to get sha256
我真的很喜欢 @villapx 回复,但我的编辑被拒绝了,所以我将发布现代 C++ 处理指针生命周期的实现。
I really liked @villapx reply but my edit was rejected so I will post modern c++ take on handling pointer lifecycle for his implementation.
如果你想计算文件的 sha256 哈希值...
If you want to compute the sha256 hash of a file...
这是我个人使用的函数 - 我只是从用于
sha-1
哈希的函数中派生出它:Here's the function I personally use - I simply derived it from the function I used for
sha-1
hashing:从 OpenSSL 3.0 开始,OpenSSL 已弃用大部分旧版(例如 SHA.h)Api,因此我不建议使用它,而是迁移到新的 OpenSSL 3.xx API
请参阅 https://www.openssl.org/docs/man3.0 /man7/migration_guide.html
现在关于您的问题:
PS:不要忘记为输出摘要分配内存,在EVP_sha256()的情况下可以使用SHA256_DIGEST_LENGTH,并且显然可以处理任何错误
OpenSSL deprecated most of the older (Ex : SHA.h) Api starting OpenSSL 3.0 so i do not recommend using it, but rather migrate to the new OpenSSL 3.xx API
see https://www.openssl.org/docs/man3.0/man7/migration_guide.html
Now regarding your question :
P.S : Do not forget to allocate memory for your output Digest, you can use SHA256_DIGEST_LENGTH in the case of EVP_sha256(), and obviously handle any errors
更“C++”的版本
A more "C++"ish version
我认为您只需将 SHA1 函数替换为 SHA256 函数,并使用帖子中链接中的 tatk 代码
I think that you only have to replace SHA1 function with SHA256 function with tatk code from link in Your post