c语言 调用md5sum,如何在C ++中获取文件的MD5哈希?

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

#include

#include

#include

#include

#include

#include

#include

#include

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

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值