Currently under the Linux system, the shell command can get the MD5 value, as follows:
If you are programming in C + +, executing shell commands in your code can be obtained, but it is very unsightly, especially if you understand the mechanism of the system or Popen function. It is now introduced to generate file MD5 using the MD5 function of OpenSSL:
#include <fstream>#include<openssl/md5.h>#include<string>usingSTD::string;intGET_FILE_MD5 (ConstSTD::string&file_name, std::string&md5_value) {md5_value.clear (); Std::ifstream file (File_name.c_str (), std::ifstream::binary); if(!file) { return-1; } Md5_ctx Md5context; Md5_init (&md5context); Charbuf[1024x768* -]; while(File.good ()) {File.read (buf,sizeof(BUF)); Md5_update (&Md5context, buf, File.gcount ()); } unsignedCharResult[md5_digest_length]; Md5_final (Result,&md5context); Charhex[ *]; memset (Hex,0,sizeof(hex)); for(inti =0; i < md5_digest_length; ++i) {sprintf (hex+ I *2,"%02x", Result[i]); } hex[ +] =' /'; Md5_value=string(hex); return 0;}intMainintargcChar*argv[]) { stringfile_name ="/home/dev/test.txt"; stringMd5value; int ret = GET_FILE_MD5 (file_name, Md5value);
if (Ret < 0)
{
printf ("Get file MD5 failed. File=%s\n ", File_name.c_str ());
return-1;
} printf ("The md5value=%s\n", Md5value.c_str ());}
Linux C + + file acquisition MD5