添砖加瓦:几种常见的数据摘要算法(MD5、CRC32、SHA1和SHA256)
1、算法概述
数据摘要算法是密码学算法中非常重要的一个分支,它通过对所有数据提取指纹信息以实现数据签名、数据完整性校验等功能,由于其不可逆性,有时候会被用做敏感信息的加密。数据摘要算法也被称为哈希(Hash)算法或散列算法。
1.1、CRC8、CRC16、CRC32
#ifndef HEADER_CHECK_H
# define HEADER_CHECK_H #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
#include <openssl/sha.h> #include "zlib.h" #ifdef __cplusplus
extern "C"{
#endif #define BUFSIZE 1024*1024*2 /*
* 使用OpenSSL提供的MD5相关函数计算字符串和大文件的MD5值
*/
int calcBufMD5(unsigned char *src, size_t len,unsigned char *dst);
int calcFileMD5(const char *inFile,unsigned char *dst); /*
* 使用OpenSSL提供的SHA1 SAH256函数分别计算字符串和大文件的SAH1 SAH256值
*/
int calcBufSHA1(unsigned char *src, size_t len,unsigned char *dst);
int calcFileSHA1(const char *inFile,unsigned char *dst); int calcBufSHA256(unsigned char *src, size_t len,unsigned char *dst);
int calcFileSHA256(const char *inFile,unsigned char *dst); /*
* 自实现CRC32校验(查表法)
* 用以计算字符串和大文件的CRC32值
*/
unsigned int calcBufCRC32(unsigned int crc,unsigned char *buf,size_t len);
unsigned int calcFileCRC32(const char *inFile); # ifdef __cplusplus
}
# endif # endif
check.c
#include "check.h"
#include <stdlib.h> //计算字符串的MD5
int calcBufMD5(unsigned char *src,size_t len,unsigned char *dst)
{
if(NULL == src || NULL == dst)
{
fprintf(stderr,"%s\n","input parameter error");
return -;
} MD5(src,len,dst); return ;
} //计算大文件的MD5值
int calcFileMD5(const char *inFile,unsigned char *dst)
{
if(NULL == inFile || NULL == dst)
{
fprintf(stderr,"%s\n","input parameter error");
return -;
} char buf[BUFSIZE] = {}; int nread;
MD5_CTX ctx;
FILE *fin = fopen(inFile,"r");
if(NULL == fin)
{
fprintf(stderr,"%s\n","open file error");
return -;
} MD5_Init(&ctx);
while((nread = fread(buf,,BUFSIZE,fin)) > )
{
MD5_Update(&ctx,buf,nread);
} MD5_Final(dst,&ctx); return ;
} //计算字符串的SHA1
int calcBufSHA1(unsigned char *src, size_t len,unsigned char *dst)
{
if(NULL == src || NULL == dst)
{
fprintf(stderr,"%s\n","input parameter error");
return -;
} unsigned char sha[] = {};
char tmp[] = {};
int i; SHA1(src,len,dst); for(i = ; i < ; i++)
{
sprintf(tmp,"%02x",sha[i]);
strcat((char*)dst,tmp);
} return ;
}
//计算大文件的SAH1
int calcFileSHA1(const char *inFile,unsigned char *dst)
{
if(NULL == inFile || NULL == dst)
{
fprintf(stderr,"%s\n","input parameter error");
return -;
} char buf[BUFSIZE] = {};
unsigned char sha[] = {};
char tmp[] = {};
int i,nread;
SHA_CTX ctx;
FILE *fin = fopen(inFile,"r");
if(NULL == fin)
{
fprintf(stderr,"%s\n","open file error");
return -;
} SHA1_Init(&ctx);
while((nread = fread(buf,,BUFSIZE,fin)) > )
{
SHA1_Update(&ctx,buf,nread);
}
SHA1_Final(dst,&ctx); fclose(fin);
return ;
} //计算字符串的SAH256
int calcBufSHA256(unsigned char *src, size_t len,unsigned char *dst)
{
if(NULL == src || NULL == dst)
{
fprintf(stderr,"%s\n","input parameter error");
return -;
} unsigned char sha[] = {};
char tmp[] = {};
int i; SHA256(src,len,dst);
return ;
}
//计算大文件的SAH256
int calcFileSHA256(const char *inFile,unsigned char *dst)
{
if(NULL == inFile || NULL == dst)
{
fprintf(stderr,"%s\n","input parameter error");
return -;
} char buf[BUFSIZE] = {};
unsigned char sha[] = {};
char tmp[] = {};
int i,nread;
SHA256_CTX ctx;
FILE *fin = fopen(inFile,"r");
if(NULL == fin)
{
fprintf(stderr,"%s\n","open file error");
return -;
} SHA256_Init(&ctx);
while((nread = fread(buf,,BUFSIZE,fin)) > )
{
SHA256_Update(&ctx,buf,nread);
}
SHA256_Final(dst,&ctx); fclose(fin); return ;
} //计算字符串的CRC32
unsigned int calcBufCRC32(unsigned int crc,unsigned char *buf,size_t len)
{
return crc32(crc,buf,len);
}
//计算大文件的CRC32
unsigned int calcFileCRC32(const char *inFile)
{
int nread;
unsigned char buf[BUFSIZE] = {};
unsigned int crc = ; FILE *fin = fopen(inFile,"rb");
if(NULL == fin)
{
fprintf(stderr,"%s\n","open file error");
return -;
} while((nread = fread(buf,,BUFSIZE,fin)) > )
crc = calcBufCRC32(crc,buf,nread); fclose(fin); return crc;
}
test.c
#include <stdio.h>
#include <sys/time.h>
#include "check.h" int main(int argc,char *argv[])
{
unsigned char *data = ""; struct timeval start;
struct timeval end;
double diff; unsigned char *md = (unsigned char *)malloc();
if(NULL == md)
{
fprintf(stderr,"%s\n","malloc error");
return -;
} memset(md,,sizeof(md));
gettimeofday(&start,NULL);
calcFileMD5(argv[],md);
gettimeofday(&end,NULL); diff = (end.tv_sec - start.tv_sec)* + (end.tv_usec - start.tv_usec); printf("%s MD5:%s\n",argv[],md);
for(int i = ; i< ;i++)
printf("%02x",md[i]);
printf("spend time :%fs\n\n",diff/); diff =;
gettimeofday(&start,NULL);
unsigned crc = calcFileCRC32(argv[]);
gettimeofday(&end,NULL); diff = (end.tv_sec - start.tv_sec)* + (end.tv_usec - start.tv_usec); printf("%s CRC32:%u\n",argv[],crc);
printf("spend time :%fs\n\n",diff/); diff = ;
memset(md,,sizeof(md));
gettimeofday(&start,NULL);
calcFileSHA1(argv[],md);
gettimeofday(&end,NULL); diff = (end.tv_sec - start.tv_sec)* + (end.tv_usec - start.tv_usec); printf("%s SHA1:%s\n",argv[],md);
printf("spend time :%fs\n\n",diff/); diff =;
memset(md,,sizeof(md));
gettimeofday(&start,NULL);
calcFileSHA256(argv[],md);
gettimeofday(&end,NULL); diff = (end.tv_sec - start.tv_sec)* + (end.tv_usec - start.tv_usec); printf("%s SHA256:%s\n",argv[],md);
printf("spend time :%fs\n\n",diff/); free(md); return ;
}
说明:经过MD5、SHA1和SHA256计算出的校验值需要再经过一层转换,才能成为可识别的字符串,以MD5为例:
for(int i = ; i< ;i++)
printf("%02x",md[i]);
添砖加瓦:几种常见的数据摘要算法(MD5、CRC32、SHA1和SHA256)的更多相关文章
- Java 常见摘要算法——md5、sha1、sha256
目录 摘要算法简介 md5 使用jdk内置方法实现md5加密 使用bc方式实现md5加密 使用cc方式实现md5加密 sha1 使用jdk内置方法实现sha1加密 使用bc方式实现sha1加密 使用c ...
- App6种常见的数据加载设计
App6种常见的数据加载设计 设计师在进行APP设计的设计时,往往会更加专注于界面长什么样,界面和界面之间怎么跳转,给予用户什么样的操作反馈,却偏偏特别容易忽略掉一个比较重要的环节,就是APP数据加载 ...
- 2.Hive的几种常见的数据导入方式
好久没写Hive的那些事了,今天开始写点吧.今天的话题是总结Hive的几种常见的数据导入方式,我总结为四种:(1).从本地文件系统中导入数据到Hive表:(2).从HDFS上导入数据到Hive表:(3 ...
- MD5,SHA1及SHA256等哈希加密方法实现:Java,C#,Golang,Python
哈希算法又称散列算法,它可以从任何数据中快速的创建一个凭证,而这个凭证很难被推倒出来,因为一丁点的变化会导致凭证的差别恨到,也就是说哈希算法具有不可逆性,因此它在密码数据校验方面用的很广,比如我们常用 ...
- 摘要算法CRC8、CRC16、CRC32,MD2 、MD4、MD5,SHA1、SHA256、SHA384、SHA512,RIPEMD、PANAMA、TIGER、ADLER32
1.CRC8.CRC16.CRC32 CRC(Cyclic Redundancy Check,循环冗余校验)算法出现时间较长,应用也十分广泛,尤其是通讯领域,现在应用最多的就是 CRC32 算法,它产 ...
- Python中摘要算法MD5,SHA1讲解
摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示).摘要算法就是通过摘要函数f()对任意长度的数据data计算出固定长度的摘要di ...
- .net实现md5加密 sha1加密 sha256加密 sha384加密 sha512加密 des加密解密
写项目时,后台一直用md5加密,一天群里人问,除了MD5还有其它的加密方法吗?当时只知道还有个SHA,但怎么实现什么的都不清楚,于是当网上找了下,把几种常见的加密方法都整理了下,用winform写了个 ...
- android端从服务器抓取的几种常见的数据的处理方式
1.图片 public void look(View v) { String path = et_path.getText().toString(); try { URL url = new URL( ...
- win7和linux下利用命令查看文件md5、sha1、sha256
win7 certutil -hashfile <filename> MD5 certutil -hashfile <filename> SHA1 certutil -hash ...
随机推荐
- My97DatePicker日历插件
My97DatePicker具有强大的日期功能,能限制日期范围,对于编写双日历比较简便. 注意事项: My97DatePicker目录是一个整体,不可以破坏 My97DatePicker.html 是 ...
- ant design for vue select 数据回显问题
例如: 想要回显id为1的温度, 结果直接在select框中显示了1,而不是选中了温度, 此时因为select中的value是string类型, 而我们设置的id是number类型, 对应不上, 所以 ...
- OVERVIEW:gcc,g++,cmake,make
首先介绍一下GCC:GNU Compiler Collection(GNU 编译器集合),在为Linux开发应用程序时,绝大多数情况下使用的都是C语言,因此几乎每一位Linux程序员面临的首要问题都是 ...
- aiohttp web服务端(server)样例 (非client)
python版本 python3.6 (其他版本需要小改,版本>python3.4) 参考网址:https://www.cnblogs.com/ameile/p/5589808.html as ...
- 题解-------CF372C Watching Fireworks is Fun
传送门 一道有趣的DP 题目大意 城镇中有$n$个位置,有$m$个烟花要放.第$i$个烟花放出的时间记为$t_{i}$,放出的位置记为$a_{i}$.如果烟花放出的时候,你处在位置$x$,那么你将收获 ...
- opencv+tkinter制作HsvMaster(一)
这两天看opencv-python的HSV色彩空间,在写程序时发现用HSV来提取图像区域是件令人恶心的麻烦事.拿阈值分割做个对比,阈值最多也就一两个参数需要调整:但是HSV需要对三个通道调整上下限,也 ...
- HDU-1875 畅通工程再续(最小生成树+判断是否存在)
http://acm.hdu.edu.cn/showproblem.php?pid=1875 Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛 ...
- Spring加载xml配置文件的方式
梳理Spring的流程 xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory,ClassPathXmlApplica ...
- 文件操作符|-e|-M|-s|-A|_|-r -w $filename|stat|localtime|&|>>|<<
TTY:终端是一种字符型设备,它有多种类型,通常使用tty 来简称各种类型的终端设备 #!/usr/bin/perl use strict; use warnings; print "exi ...
- XMemcached 中文api
变更历史 2010-06-22 添加客户端分布和SASL验证两节,更新spring配置一节. 2010-06-23 添加maven依赖说明 2010-10-17 1.2.6 released 2011 ...