The Linux OpenSSL provides a series of hash and cryptographic functions, if calling the MD5 function provided by OpenSSL to generate an arbitrary string of MD5? The following provides a section of code to implement the Linux C string generation MD5 function.
Specific code:
| 123456789101112131415161718192021222324252627282930 |
#include "stdio.h"#include <string.h>#include <stdlib.h>#include <openssl/md5.h>intget_md5(char*input,char*output){ charpassword[1024*1024*5]={0}; MD5_CTX x; inti = 0, len; char*out = NULL; unsigned chard[16]; unsigned chartmp[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;} |
This article is collected and collated by www.169it.com
Two code is also provided for reference:
Method One:
| 123456789101112131415161718192021 |
#include<stdio.h>#include<openssl/md5.h>#include<string.h>intmain( intargc, char**argv ){MD5_CTX ctx;unsigned char*data= "123";unsigned charmd[16];charbuf[33]={‘\0‘};chartmp[3]={‘\0‘};inti;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);return0;} |
Output:
202cb962ac59075b964b07152d234b70
Method Two:
| 1234567891011121314151617 |
#include<stdio.h>#include<openssl/md5.h>#include<string.h>intmain( intargc, char**argv ){unsigned char*data = "123";unsigned charmd[16];inti;chartmp[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;} |
Output:
202cb962ac59075b964b07152d234b70
Article Source:Linux C based on OpenSSL generation MD5 function
Linux c based on OpenSSL generates MD5 functions