openssl初步使用
centos平台
md5.c
#include <stdio.h> #include <string.h> #include <stdlib.h> //#include "../e_os.h" #include <openssl/evp.h> #include <openssl/md5.h> static char *hex2str(unsigned char *md) { int i; static char buf[80]; for (i = 0; i < MD5_DIGEST_LENGTH; i++) sprintf(&(buf[i * 2]), "%02x", md[i]); return (buf); } int main() { unsigned char md5Result[MD5_DIGEST_LENGTH]; char *source = "000000"; char *p; if (!EVP_Digest(source, strlen(source), md5Result, NULL, EVP_md5(),NULL)){ fprintf(stderr,"err"); return -1; } p = hex2str(md5Result); printf("md5(%s)=%s\n",source,p); return 0; }
makefile
CC=gcc CFLAGS=-Wall -g CINC=-I. -I/home/liuyi/openssl/include LIBS=-L/home/liuyi/openssl/lib LDFLAGS=-lcrypto -lssl TARGET=md5 OBJ=md5.o all:$(TARGET) $(TARGET): $(OBJ) $(CC) $(CFLAGS) -o $@ $(OBJ) $(LIBS) $(LDFLAGS) .c.o: $(CC) -c $(CFLAGS) $(CINC) $< clean: rm -f $(TARGET) *.o
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/liuyi/openssl/lib
export LD_LIBRARY_PATH
./md5