eladm26 eladm26 -4 years ago 160
Linux Question

how to get md5sum command and get the string output in code

I'm writing a code in C++ and I wish to get the md5 checksum of a certain string.
I know I can run

system("echo "myStirng" | md5sum > some_file.txt")
and then read the file.
I'm writing code for an embedded system and due to performance issues I don't wan't to use file operations. also, I can't use any openSSL functions in my code.
I did my searching and all the answers I came across either included some file operations or used openSSL functions.
Is there a way to get the md5 checksum without using file operations and without using openSSL? maybe running the linux command md5sum as a different process from the code like the C function
system
does, but returning the checksum hex string and not the return code.

Thanks you.

Answer Source

This is a proof of concept I wrote some time ago. Pretty much like the second answer in the link in @slugonmission 's comment but a bit better I think (using fread). Actually at the end I preferred the approach using the library as a little bit faster. So I suggest if dependency on openssl is ok with you and performance is important to prefer the library.

FILE* pipe = popen("md5sum /home/bla.txt", "r");                                                                                       
if (pipe != NULL)                                                                                                                                                    
{                                                                                                                                                     
  static const unsigned md5size = 32;                                                                                                                 
  unsigned char md5res[md5size + 1];                                                                                                                  
  int readSize = fread((void*) md5res, sizeof(char), md5size, pipe);                                                                                  
  if (readSize != md5size)                                                                                                                            
  {                                                                                                                                                    
      std::cout << "Error reading md5" << std::endl;                                                                                                     
      return 1;                                                                                                                                          
  }                                                                                                                                                    
  pclose(pipe);                                                                                                                        
  md5res[md5size] = 0;    
 }                                                                                                                            
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download