-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.

3
  • 1
    Have a look at stackoverflow.com/questions/3395690/md5sum-of-file-in-linux-c Commented Dec 5, 2013 at 11:33
  • @slugonamission this solution will require me to download and install openSSL, is there a way to avoid it? Commented Dec 5, 2013 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. Commented Dec 5, 2013 at 11:56

2 Answers 2

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; } 
Sign up to request clarification or add additional context in comments.

1 Comment

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?
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"); } 

1 Comment

Thank you for the help but I can't use this library.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.