说明:
数字摘要是将任意长度的消息变成固定长度的短消息,它类似于一个自变量是消息的函数,也就是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[] = {};
int len = ; 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, , , fp)) > )
{
MD5_Update(&ctx, buf, len);
memset(buf, , len);
}
MD5_Final(dgst, &ctx);
}
void calc_fileSHA(const char *filename, unsigned char *dgst)
{
SHA_CTX ctx;
char buf[] = {};
int len = ; 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, , , fp)) > )
{
SHA1_Update(&ctx, buf, len);
memset(buf, , len);
}
SHA1_Final(dgst, &ctx);
}
void calc_fileSHA224(const char *filename, unsigned char *dgst)
{
SHA256_CTX ctx;
char buf[] = {};
int len = ; 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, , , fp)) > )
{
SHA224_Update(&ctx, buf, len);
memset(buf, , len);
}
SHA224_Final(dgst, &ctx);
}
void calc_fileSHA256(const char *filename, unsigned char *dgst)
{
SHA256_CTX ctx;
char buf[] = {};
int len = ; 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, , , fp)) > )
{
SHA256_Update(&ctx, buf, len);
memset(buf, , len);
}
SHA256_Final(dgst, &ctx);
}
void calc_fileSHA384(const char *filename, unsigned char *dgst)
{
SHA512_CTX ctx;
char buf[] = {};
int len = ; 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, , , fp)) > )
{
SHA384_Update(&ctx, buf, len);
memset(buf, , len);
}
SHA384_Final(dgst, &ctx);
}
void calc_fileSHA512(const char *filename, unsigned char *dgst)
{
SHA512_CTX ctx;
char buf[] = {};
int len = ; 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, , , fp)) > )
{
SHA512_Update(&ctx, buf, len);
memset(buf, , len);
}
SHA512_Final(dgst, &ctx);
} void printDgst(unsigned char *dgst, size_t len)
{
if (NULL == dgst || len <= )
{
printf("Input error...\n");
return;
} for (size_t i = ; i < len; ++i)
printf("%x", dgst[i]);
printf("\n");
}