使用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, strlendata));
    md5_finish&md5StateT, digest);
    char md5String[33] = { '' }, hexBuffer[3];
    for size_t i = 0; i != 16; ++i)
    {
        if digest[i] < 16)
            sprintfhexBuffer, "0%X", digest[i]);
        else
            sprintfhexBuffer, "%X", digest[i]);
        strcatmd5String, 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 = fopenfilename, "rb");
    if file)
    {
        printf"正在计算MD5值...
");
        time_t startTime = timeNULL);
        md5_state_t md5StateT;
        md5_init&md5StateT);
        char buffer[INPUT_BUFFER_SIZE];
        while !feoffile))
        {
            size_t numberOfObjects = freadbuffer, sizeofchar), 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)
                sprintfhexBuffer, "0%x", digest[i]);
            else
                sprintfhexBuffer, "%x", digest[i]);
            strcatmd5String, hexBuffer);
        }
        time_t endTime = timeNULL);
        fclosefile);
        printf"计算完毕:%s,耗时%u秒
", md5String, endTime - startTime);
    }
    else
        printf"无法打开文件:%s
", filename);
    system"pause");
    return 0;
}

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注