首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在C++中获取文件的MD5哈希值?

如何在C++中获取文件的MD5哈希值?
EN

Stack Overflow用户
提问于 2009-08-02 22:35:10
回答 12查看 224.4K关注 0票数 80

我有文件路径。我怎样才能得到它的MD5散列?

EN

回答 12

Stack Overflow用户

发布于 2009-08-02 23:34:50

下面是md5sum命令的直接实现,它计算并显示命令行中指定的文件的MD5。它需要链接到OpenSSL库(gcc md5.c -o md5 -lssl)才能工作。它是纯C语言,但是您应该能够很容易地使其适应您的C++应用程序。

代码语言:javascript
复制
#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);
    munmap(file_buffer, file_size); 

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

    return 0;
}
票数 53
EN

Stack Overflow用户

发布于 2009-08-02 22:43:08

您可以自己实现MD5算法(示例在web上随处可见),也可以链接到OpenSSL库并使用OpenSSL的摘要函数。下面是一个获取字节数组的MD5的示例:

代码语言:javascript
复制
#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);
}
票数 22
EN

Stack Overflow用户

发布于 2014-03-06 06:21:41

用于从"https://stackoverflow.com/questions/4393017/md5-implementation-in-c“重定向的任何人,因为它被错误地标记为副本。

位于以下位置的示例可以工作:

http://www.zedwood.com/article/cpp-md5-function

如果你正在用VC++2010编译,那么你需要把他的main.cpp改成这样:

代码语言:javascript
复制
#include <iostream> //for std::cout
#include <string.h> //for std::string
#include "MD5.h"

using std::cout; using std::endl;

int main(int argc, char *argv[])
{
    std::string Temp =  md5("The quick brown fox jumps over the lazy dog");
    cout << Temp.c_str() << endl;

    return 0;
}

如果要读取char *数组而不是字符串来回答此页面上的问题,则必须稍微更改MD5类。

编辑:

显然,修改MD5库并不清楚,为了方便起见,这里提供了一个完整的VC++2010解决方案,其中包含了char *:

https://github.com/alm4096/MD5-Hash-Example-VS

这里有一个简单的解释:

代码语言:javascript
复制
#include <iostream> //for std::cout
#include <string.h> //for std::string
#include <fstream>
#include "MD5.h"

using std::cout; using std::endl;

int main(int argc, char *argv[])
{
    //Start opening your file
    ifstream inBigArrayfile;
    inBigArrayfile.open ("Data.dat", std::ios::binary | std::ios::in);

    //Find length of file
    inBigArrayfile.seekg (0, std::ios::end);
    long Length = inBigArrayfile.tellg();
    inBigArrayfile.seekg (0, std::ios::beg);    

    //read in the data from your file
    char * InFileData = new char[Length];
    inBigArrayfile.read(InFileData,Length);

    //Calculate MD5 hash
    std::string Temp =  md5(InFileData,Length);
    cout << Temp.c_str() << endl;

    //Clean up
    delete [] InFileData;

    return 0;
}

我只需将以下代码添加到MD5库中:

MD5.cpp:

代码语言:javascript
复制
MD5::MD5(char * Input, long length)
{
  init();
  update(Input, length);
  finalize();
}

MD5.h:

代码语言:javascript
复制
std::string md5(char * Input, long length);
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1220046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档