在网上搜集了好几个C++的MD5算法源代码,调试通过后计算出的值看起来是MD5,实际却不是,产生了部分偏移。做C++程序用这些代码的话,肯定无法达到使用MD5验证的目的。
尔后发现 linux shell 有直接的命令 可以 获取 字符串/文件的MD5,就通过使用 popen进行交互获取MD5值,如下:
理论上,能使用 shell 交互的 功能都能通过 popen进行交互,例如 本人通过Python 实现了 RSA非对称加密,但是得用C++表达程序,则可以用这个方法,简单有效。
由于使用了 shell 功能,效率上肯定慢一些,在高效的场合下不推荐使用,可以试着用 Python 混编,载入Python模块调用API
以下为 该 MD5 完整代码,调试通过
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<limits.h>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
void *string2char(string theString,char re[])
{
strcpy(re,theString.c_str());
}
//e