Ubuntu/linux调用md5加密数据/文件
发布日期:2021-07-19 12:30:18
浏览次数:14
分类:技术文章
本文共 1970 字,大约阅读时间需要 6 分钟。
安装库
sudo apt-get install opensslsudo apt-get install libssl-dev
编写程序
//gcc -g str_md5_demo.c -o str_md5_demo -lcrypto//#include#include #include int main(int argc, char* argv[]){ MD5_CTX ctx; unsigned char md[16] = { 0}; int i = 0; //方法一: MD5_Init(&ctx); MD5_Update(&ctx, "hel", 3); MD5_Update(&ctx, "lo", 2); MD5_Final(md, &ctx); for (i = 0; i < 16; i++) printf("%02X", md[i]); printf("\n"); //方法二: const char data[] = "hello"; MD5(data, strlen(data), md); for (i = 0; i < 16; i++) printf("%02X", md[i]); printf("\n"); return 0;}
//gcc -g file_md5_demo.c -o file_md5_demo -lcrypto// #include#include #include int main(int argc, char* argv[]){ MD5_CTX ctx; unsigned char md[16] = { 0}; char buffer[1024] = { 0}; char filename[64] = { 0}; int len = 0, i; FILE* fp = NULL; printf("请输入文件名, 用于计算MD5值\n"); scanf("%s", filename); fp = fopen(filename, "rb"); if(NULL == fp){ printf("can't open file\n"); return 1; } //方法一: MD5_Init(&ctx); while((len=fread(buffer, 1, sizeof(buffer), fp)) > 0){ MD5_Update(&ctx, buffer, len); memset(buffer, 0 ,sizeof(buffer)); } MD5_Final(md, &ctx); for(i=0; i<16; i++) printf("%02X", md[i]); printf("\n"); //方法二: while((len=fread(buffer, 1, sizeof(buffer), fp)) > 0){ MD5(buffer, len, md); memset(buffer, 0 ,sizeof(buffer)); } for(i=0; i<16; i++) printf("%02X", md[i]); printf("\n"); if(fp) fclose(fp); return 0;}
编写Makefile
注意:在Ubuntu 14.04 64bit上一定要需要链接 -lcrypto
INCLUDES = -I. -I /usr/include/http-testSRCS = http-test.cOBJS = $(SRCS:.c=.o)CC = gccCFLAGS = -Wall -O -gLIBS = -lpthread -lcryptoEXE = http-test$(EXE):$(OBJS) $(CC) $^ -o $@ $(LIBS)%.o: %.c $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@clean: rm -f $(EXE) $(OBJS)
转载地址:https://blog.csdn.net/jdsnpgxj/article/details/107019967 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月10日 11时58分58秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Unity 5 AudioMixer
2019-04-27
Unity 代码混淆: CodeGuard的使用
2019-04-27
UGUI 列表循环使用
2019-04-27
使用命令行运行unity并执行某个静态函数(运用于命令行打包和批量打包)
2019-04-27
web.py框架
2019-04-27
web.py学习笔记
2019-04-27
python的代码缩进
2019-04-27
A* Pathfinding Project (Unity A*寻路插件) 使用教程
2019-04-27
bash学习笔记
2019-04-27
sqlite学习
2019-04-27
手把手教你实现Unity与Android的交互
2019-04-27
手把手教你使用Unity的Behavior Designer
2019-04-27
Unity3D摄像机裁剪——NGUI篇
2019-04-27
lua深拷贝一个table
2019-04-27
app运行提示Unable to Initialize Unity Engine
2019-04-27
spring boot 与 Ant Design of Vue 实现修改按钮(十七)
2019-04-27
spring boot 与 Ant Design of Vue 实现删除按钮(十八)
2019-04-27
spring boot 与 Ant Design of Vue 实现新增角色(二十)
2019-04-27
spring boot 与 Ant Design of Vue 实现修改角色(二十一)
2019-04-27