C语言通过openssl库计算指定文件的MD5码

1. 在linux终端下可以通过md5sum命令计算文件的MD5,命令如下:

2. 在c代码中可以通过openssl库计算指定文件的MD5。

2.1 主要用到如下API:

a) int MD2_Init(MD2_CTX *c);

b) int MD2_Update(MD2_CTX *c,
                  const unsigned char *data,
                  unsigned long len);
c) int MD2_Final(unsigned char *md,
                 MD2_CTX *c);

2.2 编译:

gcc md5sum.c -lcrypto

2.3 源代码md5sum.c

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <openssl/md5.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int md5_checksum(const char *filename, char *output) 
{                                                    
        int ret = 0;
        int file_len = 0, read_len = 0;     
        int loop = 0;                                   
        FILE *fp = NULL;                             
        char *buf = NULL;                            
        unsigned char digest[16];                    
        struct stat stat_buf;                        
        MD5_CTX context;                             
                                                     
    	memset(&stat_buf, 0, sizeof(struct stat));
        ret = stat(filename, &stat_buf);
        if (ret < 0) {
                perror("stat");
                ret = -1;
                goto errexit;
        }                                            
        file_len = stat_buf.st_size;

        buf = malloc(file_len);                      
        if (NULL == buf) {
                perror("malloc");
                return -1;
        }
        memset(buf, 0, file_len);                    
                                                     
        fp = fopen(filename, "rb");                  
        if (NULL == fp) {                            
                perror("fopen");                     
                ret = -1;
                goto errexit;
        }
        read_len = fread(buf, 1, file_len, fp);

        MD5_Init(&context);
        MD5_Update (&context, buf, read_len);
        MD5_Final (digest, &context);

        for (loop = 0; loop < sizeof(digest) / sizeof(char); loop++) {
                sprintf( &(output[loop * 2]), "%02x", (unsigned char)digest[loop]);
                sprintf( &(output[loop * 2 + 1]), "%02x", (unsigned char)(digest[loop]<<4));
        }

        *(output + strlen(output) - 1) = '\0';
errexit:
        if (NULL != fp) {
            fclose(fp);
            fp = NULL;
        }
        if (NULL != buf) {
            free (buf);
            buf = NULL;
        }
        return ret;                                                            
}

int main(int argc, char *argv[])
{
    int ret = 0;
    char output[64] = {0};

    if (2 != argc) {
        printf("Usage: ./a.out FILENAME\n.");
        return -1;
    }

    ret = md5_checksum(argv[1], output);
    printf("%s\n", output);

    return ret;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值