您的位置:首页 > 运维架构 > Linux

Linux MD5 programming in C Language

2009-06-26 11:39 302 查看
Linux 调用 openssl 的 libcrypto.so* 库加密字符串

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
int main(int argc, char *argv[])
{
char password[68] = {0};
MD5_CTX x;
int i = 0, len;
char *out = NULL;
unsigned char d[16];
unsigned char tmp[128] = { 0 };
if (argc != 2) {
printf("Usage: %s <message>/n", argv[0]);
return -1;
}
strcpy(password, argv[1]);

MD5_Init(&x);
MD5_Update (&x, (char *) password, strlen(password));
MD5_Final(d, &x);
out = 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]);
}
out[32] = 0;
printf ("%s/n", out);
return 0;
}
编译的时候记得调用 libcrypt.so :
gcc -Wall -o md5 md5.c -lcrypto
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: