当前位置:编程学堂 > OpenSSL密码算法库: MD5示例小程序

OpenSSL密码算法库: MD5示例小程序

  • 发布:2023-10-05 11:06

-->

OpenSSL http://www.sychzs.cn/ OpenSSL整个软件包大概可以分成三个主要的功能部分:密码算法库、SSL协议库以及应用程序。OpenSSL 的密码算法库包含多种加密算法的实现,可单独应用。

OpenSSL 下载:http://www.sychzs.cn/source/

安装:

./config --prefix=/data/chenzhenjing/local

make (若编译不过,make clean后重试)

make install

一个利用OpenSSL MD5算法的简单示例程序:功能:根据文本文件的地一个非空字符串进行hash

/*
* =====================================================================================
*
* Filename: SplitProduct.c
*
* Description:
*
* Version: 1.0
* Created: 04/03/2013 04:49:06 PM CST
* Revision: none
* Compiler:
* gcc -std=c99 -I/data/chenzhenjing/local/include/openssl/ -c SplitProduct_md5.c
* gcc -std=c99 -o test_md5 SplitProduct_md5.o /data/chenzhenjing/local/lib/libcrypto.a
*
* Author: Zhenjing Chen (zhenjing), support@www.sychzs.cn
* Company:
*
* =====================================================================================
*/ #define _GNU_SOURCE
#include
#include
#include
#include
#include
#include "openssl/md5.h" MD5_CTX md5_ctx; static int MD5mod(const char* str, int length, int mod){
char sign[] = {}; MD5_Init(&md5_ctx);
MD5_Update(&md5_ctx, str, length);
MD5_Final(sign, &md5_ctx); int sum = ;
for (int i=; i < ; i ++) {
sum += (sign[i]&0xff);
}
int offset = sum % mod;
return offset;
} int main(int argc, char** argv)
{
if( argc < ){
fprintf(stderr, "%s num infile outfile\n", argv[]);
exit(-);
}
int num = atoi(argv[]) ;
if( num <= ){
fprintf(stderr, "ERROR: num error: %s\n", argv[]);
exit(-);
} FILE* in = fopen(argv[], "r");
if( in == NULL){
perror("fopen");
fprintf(stderr, "ERROR: infile error: %s\n", argv[]);
exit(-);
} FILE** OUT = (FILE**)malloc(sizeof(FILE*) * num);
for(int i=; i char buf[] = {};
sprintf(buf, "%s_%d", argv[], i);
OUT[i] = fopen(buf, "w");
if( OUT[i] == NULL){
perror("fopen");
fprintf(stderr, "ERROR: infile error: %s\n", argv[]);
exit(-);
}
} size_t len = ;
ssize_t read;
char * line = NULL; while ((read = getline(&line, &len, in)) != -) {
int klen = ;
while( klen < read ){
if( isspace( *(line+klen)) ) break; klen++;
} // char id[256]={0};
// strncpy(id, line, klen);
// printf("id=%s\tklen=%d\tread=%ld\tline=%s", id, klen, read, line); fprintf(OUT[MD5mod(line, klen, num)], "%s", line);
} if(line) free(line); return ;
}

其他参考资料:

使用 OpenSSL API 进行安全编程:http://www.sychzs.cn/developerworks/cn/linux/l-openssl.html

-->

相关文章