zoukankan      html  css  js  c++  java
  • OpenSSL密码算法库: MD5示例小程序

    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
    ", argv[0]);
            exit(-1);
        }
        int num = atoi(argv[1]) ;
        if( num <= 0){
            fprintf(stderr, "ERROR: num error: %s
    ", argv[1]);
            exit(-1);
        }
    
        FILE* in = fopen(argv[2], "r");
        if( in == NULL){
            perror("fopen");
            fprintf(stderr, "ERROR: infile error: %s
    ", 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
    ", 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	klen=%d	read=%ld	line=%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

  • 相关阅读:
    【SSM 8】spring集成Mybatis通用Mapper
    【SSM 7】Mybatis底层封装思路
    【SSM 7】Mybatis底层封装思路
    【SSM 6】Spring+SpringMVC+Mybatis框架搭建步骤
    【SSM 6】Spring+SpringMVC+Mybatis框架搭建步骤
    【SSM 5】Mybatis分页插件的使用
    【SSM 5】Mybatis分页插件的使用
    (2018干货系列十)最新android开发学习路线整合
    (2018干货系列九)最新软件测试学习路线整合
    (2018干货系列八)最新VR学习路线整合
  • 原文地址:https://www.cnblogs.com/zhenjing/p/openssl.html
Copyright © 2011-2022 走看看