Hello
I have a procedure that gets an md5sum result and prints it out to console. How can I get the results into a string?
This is what I am looking for
Code:
string get_md5_sum(unsigned char* md) {
string retval = "";
int i;
for(i=0; i <MD5_DIGEST_LENGTH; i++) {
printf("%02x",md[i]);
retval += md[i]; ////// this is not right because I have to convert it via %02x, how do i do this?
}
return retval;
}
This is my full code
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <openssl/md5.h>
using namespace std;
unsigned char result[MD5_DIGEST_LENGTH];
void print_md5_sum(unsigned char* md) {
int i;
for(i=0; i <MD5_DIGEST_LENGTH; i++) {
printf("%02x",md[i]);
}
}
void get_md5_sum(unsigned char* md) {
int i;
for(i=0; i <MD5_DIGEST_LENGTH; i++) {
printf("%02x",md[i]);
}
}
// Get the size of the file by its file descriptor
unsigned long get_size_by_fd(int fd) {
struct stat statbuf;
if(fstat(fd, &statbuf) < 0) exit(-1);
return statbuf.st_size;
}
int main()
{
int file_descript;
unsigned long file_size;
char* file_buffer;
string sFilePath = "myfilepath";
file_descript = open(sFilePath.c_str(), O_RDONLY);
if(file_descript < 0) exit(-1);
file_size = get_size_by_fd(file_descript);
printf("file size:\t%lu\n", file_size);
MD5((unsigned char*) file_buffer, file_size, result);
print_md5_sum(result);
string sIn;
cin >> sIn;
return 0;
}
Thanks in advance.