zoukankan      html  css  js  c++  java
  • 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...
    ");
            return;
        }
    
        FILE *fp = fopen(filename, "rb");
        if (NULL == fp)
        {
            printf("open file:%s error...
    ", 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...
    ");
            return;
        }
    
        FILE *fp = fopen(filename, "rb");
        if (NULL == fp)
        {
            printf("open file:%s error...
    ", 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...
    ");
            return;
        }
    
        FILE *fp = fopen(filename, "rb");
        if (NULL == fp)
        {
            printf("open file:%s error...
    ", 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...
    ");
            return;
        }
    
        FILE *fp = fopen(filename, "rb");
        if (NULL == fp)
        {
            printf("open file:%s error...
    ", 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...
    ");
            return;
        }
    
        FILE *fp = fopen(filename, "rb");
        if (NULL == fp)
        {
            printf("open file:%s error...
    ", 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...
    ");
            return;
        }
    
        FILE *fp = fopen(filename, "rb");
        if (NULL == fp)
        {
            printf("open file:%s error...
    ", 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...
    ");
            return;
        }
    
        for (size_t i = 0; i < len; ++i)
            printf("%x", dgst[i]);
        printf("
    ");
    }
    View Code

     

  • 相关阅读:
    Linux内核使用的GNUC扩展
    linux常用命令--开发调试篇
    代码示例_poll的多路复用
    硬件_红外传感器
    硬件_霍尔感应器
    全志_功能引脚配置_sys_config.fex
    知识_嵌入式常用词汇
    代码示例_Input 按键驱动
    Vmware_安装_tools
    Ubunt_配置_start
  • 原文地址:https://www.cnblogs.com/lianshuiwuyi/p/9489748.html
Copyright © 2011-2022 走看看