代码改变世界

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

  zhenjing  阅读(8130)  评论(0编辑  收藏  举报

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

OpenSSL 下载:http://www.openssl.org/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), zhenjing.chen@gmail.com
 *        Company:  
 *
 * =====================================================================================
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "openssl/md5.h"

MD5_CTX  md5_ctx;

static int MD5mod(const char* str, int length, int mod){
    char sign[16] = {0};
    
    MD5_Init(&md5_ctx);
    MD5_Update(&md5_ctx, str, length);
    MD5_Final(sign, &md5_ctx);

    int sum = 0;
    for (int i=0; i < 16; i ++) {
        sum += (sign[i]&0xff);
    }
    int offset = sum % mod;
    return offset;
}

int main(int argc, char** argv)
{
    if( argc < 4){
        fprintf(stderr, "%s num infile outfile\n", argv[0]);
        exit(-1);
    }
    int num = atoi(argv[1]) ;
    if( num <= 0){
        fprintf(stderr, "ERROR: num error: %s\n", argv[1]);
        exit(-1);
    }

    FILE* in = fopen(argv[2], "r");
    if( in == NULL){
        perror("fopen");
        fprintf(stderr, "ERROR: infile error: %s\n", argv[2]);
        exit(-1);
    }

    FILE** OUT = (FILE**)malloc(sizeof(FILE*) * num);
    for(int i=0; i<num; ++i){
        char buf[256] = {0};
        sprintf(buf, "%s_%d", argv[3], i);
        OUT[i] = fopen(buf, "w");
        if( OUT[i] == NULL){
            perror("fopen");
            fprintf(stderr, "ERROR: infile error: %s\n", argv[2]);
            exit(-1);
        }
    }

    size_t len = 0;
    ssize_t read;
    char * line = NULL;

    while ((read = getline(&line, &len, in)) != -1) {
        int  klen = 0;
        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 0;
}
复制代码

其他参考资料:

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

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2012-06-27 folly学习心得
点击右上角即可分享
微信分享提示