如何在 C++ 中使用 openssl/md5哈希字符串?

发布于 2024-12-11 11:56:33 字数 425 浏览 0 评论 0原文

我需要在程序中使用 md5 算法对字符串进行哈希处理。 有 lib openssl 但我是它的新手。 如何使用它对字符串进行哈希处理?我在哪里可以找到一个好的文档来教我如何使用这个库,以及 aes 等其他函数?

我试过这段代码:

int main()
{
    unsigned char result[MD5_DIGEST_LENGTH];
    const unsigned char* str;
    str = (unsigned char*)"hello";
    unsigned int long_size = 100;
    MD5(str,long_size,result);
}

但编译器告诉我: 对 MD5 的未定义引用。

为什么存在对 MD5 的未定义引用?

I need to hash with md5 algorithm a string in my program.
There is the lib openssl but I'm a newbie about it.
How is possible hash a string using that and where I can find a good doc that teach me how to use this lib, also with other function like aes?

I've tried this code:

int main()
{
    unsigned char result[MD5_DIGEST_LENGTH];
    const unsigned char* str;
    str = (unsigned char*)"hello";
    unsigned int long_size = 100;
    MD5(str,long_size,result);
}

But the compiler told me:
undefined reference to MD5.

Why is there and undefined reference to MD5?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2024-12-18 11:56:33

您应该查看文档。一种选择是使用此功能:

#include <openssl/md5.h>
unsigned char *MD5(const unsigned char *d, 
                   unsigned long n,
                   unsigned char *md);

他们对此表示:

MD2()、MD4() 和 MD5() 计算 dn 字节的 MD2、MD4 和 MD5 消息摘要,并将其放入 < code>md (必须有 MD2_DIGEST_LENGTH == MD4_DIGEST_LENGTH == MD5_DIGEST_LENGTH == 16 字节输出的空间)。如果md为NULL,则摘要被放置在静态数组中。

至于 AES,如果您还想使用 OpenSSL,请查看 EVP 文档此示例说明如何使用它。但请注意,您必须

#define AES_BLOCK_SIZE 16

在文件顶部添加 In 才能使其正常工作。

顺便提一句。我真的可以推荐 Crypto++ 库,因为它很棒并且具有各种加密原语; AES、椭圆曲线、MAC、公钥加密等。

You should take a look at the documentation. An option is to use this function:

#include <openssl/md5.h>
unsigned char *MD5(const unsigned char *d, 
                   unsigned long n,
                   unsigned char *md);

To which they state:

MD2(), MD4(), and MD5() compute the MD2, MD4, and MD5 message digest of the n bytes at d and place it in md (which must have space for MD2_DIGEST_LENGTH == MD4_DIGEST_LENGTH == MD5_DIGEST_LENGTH == 16 bytes of output). If md is NULL, the digest is placed in a static array.

As for AES, if you also want to use OpenSSL, then take a look at EVP doc and this example of how to use it. Just note that you have to add

#define AES_BLOCK_SIZE 16

In the top of the file for it to work, though.

Btw. I can really recommend the Crypto++ library since it's great and has all kinds of cryptographic primitives; AES, Elliptic Curves, MAC, public-key crypto and so on.

薄凉少年不暖心 2024-12-18 11:56:33

MD5 函数现已弃用,因此,如果您想避免代码中出现所有这些讨厌的警告...

以下是如何在 OpenSSL 3.0 及更高版本中使用 C++ 使用 md5 的简单示例:

#include <openssl/evp.h>
#include <cstdio>

using namespace std;

string md5(const string& content)
{
  EVP_MD_CTX*   context = EVP_MD_CTX_new();
  const EVP_MD* md = EVP_md5();
  unsigned char md_value[EVP_MAX_MD_SIZE];
  unsigned int  md_len;
  string        output;

  EVP_DigestInit_ex2(context, md, NULL);
  EVP_DigestUpdate(context, content.c_str(), content.length());
  EVP_DigestFinal_ex(context, md_value, &md_len);
  EVP_MD_CTX_free(context);

  output.resize(md_len * 2);
  for (unsigned int i = 0 ; i < md_len ; ++i)
    std::sprintf(&output[i * 2], "%02x", md_value[i]);
  return output;
}

根据: https://www.openssl.org/docs/man3.0/man3/EVP_DigestInit_ex.html

The MD5 function is now deprecated, so if you want to avoid all those nasty warnings in your code...

Here's a simple example of how to use md5 with OpenSSL 3.0 and above with C++:

#include <openssl/evp.h>
#include <cstdio>

using namespace std;

string md5(const string& content)
{
  EVP_MD_CTX*   context = EVP_MD_CTX_new();
  const EVP_MD* md = EVP_md5();
  unsigned char md_value[EVP_MAX_MD_SIZE];
  unsigned int  md_len;
  string        output;

  EVP_DigestInit_ex2(context, md, NULL);
  EVP_DigestUpdate(context, content.c_str(), content.length());
  EVP_DigestFinal_ex(context, md_value, &md_len);
  EVP_MD_CTX_free(context);

  output.resize(md_len * 2);
  for (unsigned int i = 0 ; i < md_len ; ++i)
    std::sprintf(&output[i * 2], "%02x", md_value[i]);
  return output;
}

According to: https://www.openssl.org/docs/man3.0/man3/EVP_DigestInit_ex.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文