版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wushuangge/article/details/80999819
实现Linux下c字符串生成md5
#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;
}