tags:

views:

6708

answers:

11

I've the file path. How can I get the MD5 hash of it?

Thanks

+1  A: 

See this code sample, too much code to paste here:

Sample

pb
+8  A: 

You can implement the md5 algorithm yourself (examples are all over the web), or you can link against the openssl libs and use openssl's digest functions. here's an example to get the md5 of a bytearray :

#include <openssl/md5.h>
QByteArray AESWrapper::md5 ( const QByteArray& data) {
unsigned char * tmp_hash;
tmp_hash = MD5((const unsigned char*)data.constData(), data.length(), NULL);
return QByteArray((const char*)tmp_hash, MD5_DIGEST_LENGTH);
}
OneOfOne
when using Qt (as you do), i would rather just do `return QCryptographicHash::hash(data, QCryptographicHash::Md5);` as the body of the function...
akira
When it comes to security-related stuff, never write your own implementation if the stuff out there on the net will suffice. And every single possible implementation of MD4/5 is out there, so there's really no reason to write your own.
Computer Guru
+1  A: 

Google gives, among many others, this

Yacoby
A: 
AraK
Crypto++ is a really large package and its kind of overkill if he just wants the md5 checksum
bobobobo
+3  A: 

Here's a straight forward implementation of the md5sum command that computes and displays the MD5 of the file specified on the command-line. It needs to be linked against the openssl library (gcc md5.c -o md5 -lssl) to work. It's pure C, but you should be able to adapt it to your C++ application easily enough.

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <openssl/md5.h>

unsigned char result[MD5_DIGEST_LENGTH];

// Print the MD5 sum as hex-digits.
void print_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 argc, char *argv[]) {
    int file_descript;
    unsigned long file_size;
    char* file_buffer;

    if(argc != 2) { 
         printf("Must specify the file\n");
         exit(-1);
    }
    printf("using file:\t%s\n", argv[1]);

    file_descript = open(argv[1], O_RDONLY);
    if(file_descript < 0) exit(-1);

    file_size = get_size_by_fd(file_descript);
    printf("file size:\t%lu\n", file_size);

    file_buffer = mmap(0, file_size, PROT_READ, MAP_SHARED, file_descript, 0);
    MD5((unsigned char*) file_buffer, file_size, result);

    print_md5_sum(result);
    printf("  %s\n", argv[1]);

    return 0;
}
D'Nabre
on 32bit platforms, your mmap has a limit as to how large the file can be, though it is an elegant solution to the problem. On 32bit Windows, for example, you couldn't MD5 a DVD with this code.
Chris Kaminski
+1  A: 

I have used botan to perform this operation and others before. AraK has pointed out Crypto++. I guess both libraries are perfectly valid. Now it is up to you :-).

fco.javier.sanz
+1  A: 

If anybody still in interest, where are pretty library at http://256.com/sources/md5/ with expample of use. This is the simplest lib for md5

A: 

If you have a copy of Applied Cryptography, you can copy the MD5 code from the back :)

warren
A: 

Using Crypto++, you could do the following:

#include <sha.h>
#include <iostream> 

SHA256 sha; 
while ( !f.eof() ) { 
   char buff[4096];
   int numchars = f.read(...); 
   sha.Update(buff, numchars); 
}
char hash[size]; 
sha.Final(hash); 
cout << hash <<endl; 

I have a need for something very similar, because I can't read in multi-gigabyte files just to compute a hash. In theory I could memory map them, but I have to support 32bit platforms - that's still problematic for large files.

Chris Kaminski
A: 

Check this out: http://www.zedwood.com/article/121/cpp-md5-function It answers your question

Dareen
A: 

There is a collection of implementations in C, C++ and many other languages here.

I put up an implementation here.

bobobobo