<bdo id='bIDv6'></bdo><ul id='bIDv6'></ul>
    1. <tfoot id='bIDv6'></tfoot>

      <legend id='bIDv6'><style id='bIDv6'><dir id='bIDv6'><q id='bIDv6'></q></dir></style></legend>
    2. <i id='bIDv6'><tr id='bIDv6'><dt id='bIDv6'><q id='bIDv6'><span id='bIDv6'><b id='bIDv6'><form id='bIDv6'><ins id='bIDv6'></ins><ul id='bIDv6'></ul><sub id='bIDv6'></sub></form><legend id='bIDv6'></legend><bdo id='bIDv6'><pre id='bIDv6'><center id='bIDv6'></center></pre></bdo></b><th id='bIDv6'></th></span></q></dt></tr></i><div id='bIDv6'><tfoot id='bIDv6'></tfoot><dl id='bIDv6'><fieldset id='bIDv6'></fieldset></dl></div>

      <small id='bIDv6'></small><noframes id='bIDv6'>

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

      How to get the MD5 hash of a file in C++?(如何在 C++ 中获取文件的 MD5 哈希值?)
      • <tfoot id='B5s33'></tfoot>

        <small id='B5s33'></small><noframes id='B5s33'>

        <i id='B5s33'><tr id='B5s33'><dt id='B5s33'><q id='B5s33'><span id='B5s33'><b id='B5s33'><form id='B5s33'><ins id='B5s33'></ins><ul id='B5s33'></ul><sub id='B5s33'></sub></form><legend id='B5s33'></legend><bdo id='B5s33'><pre id='B5s33'><center id='B5s33'></center></pre></bdo></b><th id='B5s33'></th></span></q></dt></tr></i><div id='B5s33'><tfoot id='B5s33'></tfoot><dl id='B5s33'><fieldset id='B5s33'></fieldset></dl></div>
          <bdo id='B5s33'></bdo><ul id='B5s33'></ul>
          <legend id='B5s33'><style id='B5s33'><dir id='B5s33'><q id='B5s33'></q></dir></style></legend>
              <tbody id='B5s33'></tbody>

              1. 本文介绍了如何在 C++ 中获取文件的 MD5 哈希值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有文件路径.我怎样才能得到它的 MD5 哈希值?

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

                推荐答案

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

                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
                ");
                            exit(-1);
                    }
                    printf("using file:	%s
                ", 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:	%lu
                ", 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
                ", argv[1]);
                
                    return 0;
                }
                

                这篇关于如何在 C++ 中获取文件的 MD5 哈希值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                Delay Loading DLLs(延迟加载 DLL)
                Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                Loading a dll from a dll?(从 dll 加载 dll?)
              2. <tfoot id='wLoOQ'></tfoot>
                    <legend id='wLoOQ'><style id='wLoOQ'><dir id='wLoOQ'><q id='wLoOQ'></q></dir></style></legend>

                        <i id='wLoOQ'><tr id='wLoOQ'><dt id='wLoOQ'><q id='wLoOQ'><span id='wLoOQ'><b id='wLoOQ'><form id='wLoOQ'><ins id='wLoOQ'></ins><ul id='wLoOQ'></ul><sub id='wLoOQ'></sub></form><legend id='wLoOQ'></legend><bdo id='wLoOQ'><pre id='wLoOQ'><center id='wLoOQ'></center></pre></bdo></b><th id='wLoOQ'></th></span></q></dt></tr></i><div id='wLoOQ'><tfoot id='wLoOQ'></tfoot><dl id='wLoOQ'><fieldset id='wLoOQ'></fieldset></dl></div>
                          <tbody id='wLoOQ'></tbody>

                      1. <small id='wLoOQ'></small><noframes id='wLoOQ'>

                        • <bdo id='wLoOQ'></bdo><ul id='wLoOQ'></ul>