如何在 C++ 中使用 openssl/md5 来散列字符串? [英] How can I use openssl/md5 in C++ to hash a string?

查看:182
本文介绍了如何在 C++ 中使用 openssl/md5 来散列字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我试过这个代码:

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 的未定义引用.

But the compiler told me: undefined reference to MD5.

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

Why is there and undefined reference to MD5?

推荐答案

你应该看看 文档.一个选项是使用此功能:

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);

他们声明:

MD2()、MD4() 和 MD5() 计算 n 字节在 d 处的 MD2、MD4 和 MD5 消息摘要并将其放入 md(必须有空间用于 MD2_DIGEST_LENGTH == MD4_DIGEST_LENGTH == MD5_DIGEST_LENGTH == 16 个字节的输出).如果 md 为 NULL,则摘要放置在静态数组中.

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.

至于AES,如果你也想用OpenSSL,那就看看EVP 文档这个例子 如何使用它.请注意,您必须添加

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.

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

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.

这篇关于如何在 C++ 中使用 openssl/md5 来散列字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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