Linux下c基于openssl生成MD5的函数
Linux下openssl提供了一系列哈希及加密的函数,如果调用openssl提供的MD5函数生成任意字符串的MD5呢?下面提供了一段代码实现Linux下c字符串生成md5的函数。
具体代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#include "stdio.h"#include <string.h>#include <stdlib.h>#include <openssl/md5.h>int get_md5( char *input, char *output){ char password[1024*1024*5]={0}; MD5_CTX x; int i = 0, len; char *out = NULL; unsigned char d[16]; unsigned char tmp[128] = { 0 }; strcpy (password,input); MD5_Init(&x); MD5_Update(&x, ( char *)password, strlen (password)); MD5_Final(d, &x); out = ( char *) malloc (35); memset (out, 0x00, 35); strcpy (out, "$1$" ); // printf("MD5(\"%s\") = ", password); for (i = 0; i < 16; i++) { sprintf (out + (i*2), "%02X" , d[i]); // 转换为32位 } out[32] = 0; // printf("%s\n", out); strcpy (output,out); free (out); return 0;} |
本文由www.169it.com搜集整理
另外提供两段代码供参考:
方法一:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include<stdio.h>#include<openssl/md5.h>#include<string.h>int main( int argc, char **argv ){MD5_CTX ctx;unsigned char *data= "123" ;unsigned char md[16];char buf[33]={ '\0' };char tmp[3]={ '\0' };int i;MD5_Init(&ctx);MD5_Update(&ctx,data, strlen (data));MD5_Final(md,&ctx);for ( i=0; i<16; i++ ){sprintf (tmp, "%02X" ,md[i]);strcat (buf,tmp);}printf ( "%s\n" ,buf);return 0;} |
输出:
202CB962AC59075B964B07152D234B70
方法二:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include<stdio.h>#include<openssl/md5.h>#include<string.h>int main( int argc, char **argv ){unsigned char *data = "123" ;unsigned char md[16];int i;char tmp[3]={ '\0' },buf[33]={ '\0' };MD5(data, strlen (data),md);for (i = 0; i < 16; i++){sprintf (tmp, "%2.2x" ,md[i]);strcat (buf,tmp);}printf ( "%s\n" ,buf);return 0;} |
输出:
202cb962ac59075b964b07152d234b70
Linux下c基于openssl生成MD5的函数的更多相关文章
- 在Linux下如何使用openssl生成RSA公钥和私钥对
在<Java实现RSA密钥对并在加解密.加签验签中应用的实例>中,我们有用Java代码生成RSA密钥对,其实在Linux操作系统中,用openssl也是很容易生成密钥对的. 一.如果在ub ...
- [转]Linux下用gcc/g++生成静态库和动态库(Z)
Linux下用gcc/g++生成静态库和动态库(Z) 2012-07-24 16:45:10| 分类: linux | 标签:链接库 linux g++ gcc |举报|字号 订阅 ...
- linux下mysql基于mycat做主从复制和读写分离之基础篇
Linux下mysql基于mycat实现主从复制和读写分离1.基础设施 两台虚拟机:172.20.79.232(主) 172.20.79.233(从) 1.1软件设施 mysql5.6.39 , my ...
- 查看Linux下*.a库文件中文件、函数、变量
查看Linux下*.a库文件中文件.函数.变量等情况在Linux 下经常需要链接一些 *.a的库文件,那怎么查看这些*.a 中包 含哪些文件.函数.变量: 1. 查看文件:ar -t xxx.a 2. ...
- Linux下Makefile的automake生成全攻略--转
http://www.yesky.com/120/1865620.shtml 作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下 ...
- Linux下Makefile的automake生成全攻略
作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的M ...
- linux下用crunch工具生成密码
crunch是一款linux下的压缩后仅仅38k的小程序,crunch程序在2004年及以前由email为的作者编写mimayin@aciiid.ath.cx,后续版本由bofh28@gmail.co ...
- Linux下三个密码生成工具
http://code.csdn.net/news/2820879 想出一个难破解且容易记的密码对不是一件简单的事情.在我为电脑设定一个新密码,或者在线注册了一个新的账号,需要输入密码的时候,脑袋就一 ...
- 查看Linux下*.a库文件中文件、函数、变量等情况
在Linux 下经常需要链接一些 *.a的库文件,那怎么查看这些*.a 中包 含哪些文件.函数.变量: 1. 查看文件:ar -t *.a 2. 查看函数.变里:nm *.a
随机推荐
- springMVC部署
一.导入springMVC所需要的jar包 下载地址:http://repo.spring.io/release/org/springframework/spring/ 二.springM ...
- mac ide
常用IDE xcode sublime text eclipse xampp + phpstorm sql客户端:sequel pro 虚拟机:parallels desktop sftp客户端:Cy ...
- SmallDateTime时间范围检查
SamllDataTime是SQL Server中的一种表达时间的类型, 精度不高但是省空间,具体的细节就查看一下MSDN的文档吧. http://msdn.microsoft.com/zh-cn/l ...
- Codeforces Round #334 (Div. 2) D. Moodular Arithmetic 环的个数
D. Moodular Arithmetic Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/60 ...
- SDWebImage内存性能优化
// 清除已过期图片 [[SDImageCache sharedImageCache] cleanDisk]; // 设置最多缓存7天时间 [[SDImageCache sharedImageCach ...
- iOS开发——UI篇OC篇&UIView/UIWindow/UIScreen/CALayer
UIView/UIWindow/UIScreen/CALayer 1.UIScreen可以获取设备屏幕的大小. 1 2 3 4 5 6 7 // 整个屏幕的大小 {{0, 0}, {320, 480} ...
- PHP实现Web Service(转)
1.OAuth OAUTH协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码) 2.SCA 服务组件 ...
- Flex4之元数据标签使用
Flex 元数据标签 1.[ArrayElementType] 使用ArrayElementType元数据标签可以让你定义数组元素的数据类型. 程序代码: [ArrayElementType(&quo ...
- Cobra —— 可视化Python虚拟机 and 图解python
http://blog.csdn.net/balabalamerobert http://blog.csdn.net/efeics/article/category/1486515 图解python ...
- win7关机命令
通过 DOS 命令 “shutdown” 来实现 Windows7 系统下的定时关机功能. 使用at 命令 at 12:00 shutdown -s,取消:at/delete shutdown -s ...