• 使用C语言获取字符串或文件的MD5值


    libmd5地址:https://sourceforge.net/projects/libmd5-rfc/

    MD5Demo1.c

    #include <stdio.h>
    #include <string.h>
    #include "md5/md5.h"
    int main()
    {
        const md5_byte_t *data = "All is well!";
        md5_byte_t digest[16];
        md5_state_t md5StateT;
        md5_init(&md5StateT);
        md5_append(&md5StateT, data, strlen(data));
        md5_finish(&md5StateT, digest);
        char md5String[33] = { '' }, hexBuffer[3];
        for (size_t i = 0; i != 16; ++i)
        {
            if (digest[i] < 16)
                sprintf(hexBuffer, "0%X", digest[i]);
            else
                sprintf(hexBuffer, "%X", digest[i]);
            strcat(md5String, hexBuffer);
        }
        printf("%s
    ", md5String);
        system("pause");
        return 0;
    }

    MD5Demo2.c

    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include "md5/md5.h"
    #define INPUT_BUFFER_SIZE 100 * 1024
    int main()
    {
        const char *filename = "E:/Packages/Win7/en_windows_7_ultimate_with_sp1_x64_dvd_u_677332.iso";
        FILE *file = fopen(filename, "rb");
        if (file)
        {
            printf("正在计算MD5值...
    ");
            time_t startTime = time(NULL);
            md5_state_t md5StateT;
            md5_init(&md5StateT);
            char buffer[INPUT_BUFFER_SIZE];
            while (!feof(file))
            {
                size_t numberOfObjects = fread(buffer, sizeof(char), INPUT_BUFFER_SIZE, file);
                md5_append(&md5StateT, buffer, numberOfObjects);
            }
            md5_byte_t digest[16];
            md5_finish(&md5StateT, digest);
            char md5String[33] = { '' }, hexBuffer[3];
            for (size_t i = 0; i != 16; ++i)
            {
                if (digest[i] < 16)
                    sprintf(hexBuffer, "0%x", digest[i]);
                else
                    sprintf(hexBuffer, "%x", digest[i]);
                strcat(md5String, hexBuffer);
            }
            time_t endTime = time(NULL);
            fclose(file);
            printf("计算完毕:%s,耗时%u秒
    ", md5String, endTime - startTime);
        }
        else
            printf("无法打开文件:%s
    ", filename);
        system("pause");
        return 0;
    }
  • 相关阅读:
    oracle查看被锁的表和被锁的进程,杀掉进程
    umlの交互图
    Window XP安装Ubuntu14.04实现Samba文件共享
    开源企业IM免费企业即时通讯ENTBOOST V2014.177版本号正式公布
    必看的 jQuery性能优化的38个建议
    正则工具类以及FinalClass
    CF772E Verifying Kingdom
    aop相关术语
    BeanFactoryAware和BeanNameAware
    spring完成自动装配
  • 原文地址:https://www.cnblogs.com/buyishi/p/9029624.html
Copyright © 2020-2023  润新知