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
复制代码

 

 

posted @   落雷  阅读(831)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示