0
点赞
收藏
分享

微信扫一扫

C++ MD5加密函数


任务:对输入的字符串进行MD5加密。

#include <string>
#include <iostream>
#include <openssl/md5.h>

using namespace std;


/**
*
*/

string MD5(const string& src )
{
MD5_CTX ctx;

string md5_string;
unsigned char md[16] = { 0 };
char tmp[33] = { 0 };

MD5_Init( &ctx );
MD5_Update( &ctx, src.c_str(), src.size() );
MD5_Final( md, &ctx );

for( int i = 0; i < 16; ++i )
{
memset( tmp, 0x00, sizeof( tmp ) );
sprintf( tmp, "%02X", md[i] );
md5_string += tmp;
}
return md5_string;
}


int main()
{
cout << MD5("fupeng") << endl;
cout << MD5("123") << endl;

return 0;
}

编译运行,编译时记得要加上 -lcrypto 链接对应的库

[work@localhost lgit]$ g++ test_md5.cpp  -lcrypto
[work@localhost lgit]$ ./a.out
89501AE6765D95E368ED0FB0D2CD9E65
202CB962AC59075B964B07152D234B70


举报

相关推荐

0 条评论