OpenSSL编程之摘要

说明:

  数字摘要是将任意长度的消息变成固定长度的短消息,它类似于一个自变量是消息的函数,也就是Hash函数。数字摘要就是采用单向Hash函数将需要加密的明文“摘要”成一串固定长度(128位)的密文这一串密文又称为数字指纹,它有固定的长度,而且不同的明文摘要成密文,其结果总是不同的,而同样的明文其摘要必定一致。常用的摘要函数有:MD5、SHA1、SHA256等。

  以下内容是采用OpenSSL中提供的摘要算法对文件内容进行摘要计算,这些方法同样适用于字符串。

 

md.h

#ifndef _MD_H_
#define _MD_H_

#include <stdio.h>
#include <string.h>

#include <openssl/md5.h>
#include <openssl/sha.h>

void calc_fileMD5(const char *filename,unsigned char *dgst);
void calc_fileSHA(const char *filename,unsigned char *dgst);
void calc_fileSHA224(const char *filename,unsigned char *dgst);
void calc_fileSHA256(const char *filename,unsigned char *dgst);
void calc_fileSHA384(const char *filename,unsigned char *dgst);
void calc_fileSHA512(const char *filename,unsigned char *dgst);

void printDgst(unsigned char* dgst,size_t len);

#endif

 

md.c

#include "md.h"

void calc_fileMD5(const char *filename, unsigned char *dgst)
{
    MD5_CTX ctx;
    char buf[4096] = {0};
    int len = 0;

    if (NULL == filename || NULL == dgst)
    {
        printf("Input error...\n");
        return;
    }

    FILE *fp = fopen(filename, "rb");
    if (NULL == fp)
    {
        printf("open file:%s error...\n", filename);
        return;
    }

    MD5_Init(&ctx);
    while ((len = fread(buf, 1, 4096, fp)) > 0)
    {
        MD5_Update(&ctx, buf, len);
        memset(buf, 0, len);
    }
    MD5_Final(dgst, &ctx);
}
void calc_fileSHA(const char *filename, unsigned char *dgst)
{
    SHA_CTX ctx;
    char buf[4096] = {0};
    int len = 0;

    if (NULL == filename || NULL == dgst)
    {
        printf("Input error...\n");
        return;
    }

    FILE *fp = fopen(filename, "rb");
    if (NULL == fp)
    {
        printf("open file:%s error...\n", filename);
        return;
    }

    SHA1_Init(&ctx);
    while ((len = fread(buf, 1, 4096, fp)) > 0)
    {
        SHA1_Update(&ctx, buf, len);
        memset(buf, 0, len);
    }
    SHA1_Final(dgst, &ctx);
}
void calc_fileSHA224(const char *filename, unsigned char *dgst)
{
    SHA256_CTX ctx;
    char buf[4096] = {0};
    int len = 0;

    if (NULL == filename || NULL == dgst)
    {
        printf("Input error...\n");
        return;
    }

    FILE *fp = fopen(filename, "rb");
    if (NULL == fp)
    {
        printf("open file:%s error...\n", filename);
        return;
    }

    SHA224_Init(&ctx);
    while ((len = fread(buf, 1, 4096, fp)) > 0)
    {
        SHA224_Update(&ctx, buf, len);
        memset(buf, 0, len);
    }
    SHA224_Final(dgst, &ctx);
}
void calc_fileSHA256(const char *filename, unsigned char *dgst)
{
    SHA256_CTX ctx;
    char buf[4096] = {0};
    int len = 0;

    if (NULL == filename || NULL == dgst)
    {
        printf("Input error...\n");
        return;
    }

    FILE *fp = fopen(filename, "rb");
    if (NULL == fp)
    {
        printf("open file:%s error...\n", filename);
        return;
    }

    SHA256_Init(&ctx);
    while ((len = fread(buf, 1, 4096, fp)) > 0)
    {
        SHA256_Update(&ctx, buf, len);
        memset(buf, 0, len);
    }
    SHA256_Final(dgst, &ctx);
}
void calc_fileSHA384(const char *filename, unsigned char *dgst)
{
    SHA512_CTX ctx;
    char buf[4096] = {0};
    int len = 0;

    if (NULL == filename || NULL == dgst)
    {
        printf("Input error...\n");
        return;
    }

    FILE *fp = fopen(filename, "rb");
    if (NULL == fp)
    {
        printf("open file:%s error...\n", filename);
        return;
    }

    SHA384_Init(&ctx);
    while ((len = fread(buf, 1, 4096, fp)) > 0)
    {
        SHA384_Update(&ctx, buf, len);
        memset(buf, 0, len);
    }
    SHA384_Final(dgst, &ctx);
}
void calc_fileSHA512(const char *filename, unsigned char *dgst)
{
    SHA512_CTX ctx;
    char buf[4096] = {0};
    int len = 0;

    if (NULL == filename || NULL == dgst)
    {
        printf("Input error...\n");
        return;
    }

    FILE *fp = fopen(filename, "rb");
    if (NULL == fp)
    {
        printf("open file:%s error...\n", filename);
        return;
    }

    SHA512_Init(&ctx);
    while ((len = fread(buf, 1, 4096, fp)) > 0)
    {
        SHA512_Update(&ctx, buf, len);
        memset(buf, 0, len);
    }
    SHA512_Final(dgst, &ctx);
}

void printDgst(unsigned char *dgst, size_t len)
{
    if (NULL == dgst || len <= 0)
    {
        printf("Input error...\n");
        return;
    }

    for (size_t i = 0; i < len; ++i)
        printf("%x", dgst[i]);
    printf("\n");
}
View Code

 

 

转载于:https://www.cnblogs.com/lianshuiwuyi/p/9489748.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值