Skip to content

Instantly share code, notes, and snippets.

@sqbing
Created September 24, 2014 05:59
Show Gist options
  • Save sqbing/2a56e80bd21623e2f7ff to your computer and use it in GitHub Desktop.
Save sqbing/2a56e80bd21623e2f7ff to your computer and use it in GitHub Desktop.
md5 encoding example
#include <stdio.h>
#include <string.h>
#include <openssl/md5.h>
#define u_char unsigned char
int main(int argc, const char *argv[])
{
u_char hashb[64], hasht[128], *text = hasht;
MD5_CTX md5;
const static u_char hex[] = "0123456789abcdef";
const u_char *input = "helloworld";
memset(hashb, 0, 64);
memset(hasht, 0, 128);
MD5_Init(&md5);
MD5_Update(&md5, input, strlen(input));
MD5_Final(hashb, &md5);
// translated to more readable format
int i = 0;
for(; i < 16; i++){
*text++ = hex[hashb[i] >> 4];
*text++ = hex[hashb[i] & 0xf];
}
*text = '\0';
printf("input: %s\n", input );
printf("output: %s\n", hasht);
/*
* same as below...
u_char md[17];
memset(md, 0, 17);
MD5(input, strlen(input), md);
printf("MD5() output: ");
for(i = 0; i < 16; i++){
printf("%2x ", md[i]);
}
printf("\n");
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment