adler32_vs_md5.c 2.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/**
 *============================================================================
 *
 *  @file           adler32_vs_md5.c
 *
 *  @brief          Adler-32 롤링 해시와 MD5의 연산 시간을 비교
 *
 *  @author         Gim Ji-Hyeon (potatogim@potatogim.net)
 *  @date           2014년 01월 10일 17시 28분 00초
 *  @version        1.0
 *  @copyright      GNU General Public License
 *
 *  Compiler        gcc
 *
 *============================================================================
 */

/**
 * OpenSSL에 대한 라이브러리 의존성이 있음
 *
 * # gcc -lcrypto -lssl -O1 adler32_vs_md5.c
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <openssl/md5.h>
#include <sys/time.h>

#define SALTS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

const int MOD_ADLER = 65521;

static uint32_t
    adler32(unsigned char *data, int32_t len)
{
    uint32_t a = 1, b = 0;
    int32_t index;

    /* Process each byte of the data in order */
    for (index = 0; index < len; ++index)
    {
        a = (a + data[index]) % MOD_ADLER;
        b = (b + a) % MOD_ADLER;
    }

    return (b << 16) | a;
}

int
    main (const int argc, const char *argv[])
{
    unsigned char   *data;
    uint32_t        adler;
    unsigned char   md5[MD5_DIGEST_LENGTH];
    struct timeval  start, finish;
    int             msec, i;

    data = alloca(strlen(SALTS) + 1);
    strcpy (data, SALTS);

    printf ("String : %s\n", data);

    gettimeofday(&start, NULL);

    for (i=0; i<100000; ++i)
    {
        adler = adler32(data, strlen(SALTS));
        data[i%26] = data[i%10];
    }

    gettimeofday(&finish, NULL);

    msec = finish.tv_sec*1000 + finish.tv_usec/1000;
    msec -= start.tv_sec*1000 + start.tv_usec/1000;

    printf ("Adler-32 : %dms\n", msec);

    memset (data, 0x0, strlen(SALTS));
    strcpy (data, SALTS);

    gettimeofday(&start, NULL);

    for (i=0; i<100000; ++i)
    {
        MD5(data, strlen(SALTS), md5);
        data[i%26] = data[i%10];
    }

    gettimeofday(&finish, NULL);

    msec = finish.tv_sec*1000 + finish.tv_usec/1000;
    msec -= start.tv_sec*1000 + start.tv_usec/1000;

    printf ("MD5      : %dms\n", msec);

    return 0;
}