-2

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.

eladm26
  • 517
  • 4
  • 23
  • 1
    Have a look at http://stackoverflow.com/questions/3395690/md5sum-of-file-in-linux-c – slugonamission Dec 05 '13 at 11:33
  • @slugonamission this solution will require me to download and install openSSL, is there a way to avoid it? – eladm26 Dec 05 '13 at 11:50
  • 1
    @eladm26 Why would it require you to download/install openSSL? You should just be able to link it like so: `-lssl -lcrypto`. –  Dec 05 '13 at 11:56

2 Answers2

0

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;    
 }                                                                                                                            
ChrisN
  • 16,635
  • 9
  • 57
  • 81
  • thank you for your answer. but this answer includes files, do you think there is option to solve it without using files, maybe using md5sum linux command somehow? – eladm26 Dec 08 '13 at 12:58
0

Use http://hashlib2plus.sourceforge.net/ then run something like:

#include <hashlibpp.h>
#include <string>

int main() {
    hashwrapper *rap = new md5wrapper();
    std::string hash = rap->getHashFromFile("README.TXT");
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54