The OpenSSL crypto Library: MD5 example of a small program

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

OpenSSL  The OpenSSL software package can be divided into three main functions: crypto library, SSL protocol library and application. Library of cryptographic algorithm OpenSSL contains a variety of encryption algorithm, can be used alone.

OpenSSL Download:

Installation:

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

Make (if the make clean compile but, try again)

make install 

A use of OpenSSL MD5 algorithm a simple example: function: hash according to a non empty string text file.

/*
 * =====================================================================================
 *
 *       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), 
 *        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;
}

Other references:

The use of OpenSSL API security programming: http://www.ibm.com/developerworks/cn/linux/l-openssl.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Clement at November 18, 2013 - 11:48 AM