MDGSF Software Engineer

[openssl] MD5

2017-01-11
mdgsf

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#include "openssl/evp.h"
#include "openssl/buffer.h"
#include "openssl/md5.h"

//openssl dgst -md5 -hex Makefile

int main()
{
    MD5_CTX ctx;
    MD5_Init(&ctx);

    FILE * file = fopen("Makefile", "rb");
    if(NULL == file) {
        return -1;
    }

    int iLen = 0;
    unsigned char aucBuffer[1024] = {0};
    while ( (iLen = fread(aucBuffer, 1, 1024, file)) > 0 )
    {
        MD5_Update(&ctx, aucBuffer, iLen);
    }

    unsigned char aucDigest[16] = {0};
    MD5_Final(aucDigest, &ctx);
    fclose(file);


    char acMd5[33] = {0};
    for (int i = 0; i < 16; i++)
    {
        char acTemp[3] = {0};
        sprintf(acTemp, "%02x", aucDigest[i]);
        strcat(acMd5, acTemp);
    }
    printf("acMd5 = %s\n", acMd5);

    return 0;
}

weixingongzhonghao

Comments

Content