md5.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 27 Dec 2013 16:00:09 +0100
changeset 167 7c6b221900bf
parent 162 fad55be2d07c
permissions -rw-r--r--
Proper Datatypes to leave the 80's
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
     1
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 45
diff changeset
     2
 * md5.c
45
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
     4
 */
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
     5
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
     6
#include <stdio.h>
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
     7
#include <stdlib.h>
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
     8
#include <string.h>
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
     9
#include <openssl/md5.h>
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    10
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    11
typedef unsigned char u_char;
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    12
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    13
int main (int argc, char **argv)
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    14
{
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    15
    char buffer[80];
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    16
    char text[80];
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    17
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    18
    unsigned char *md5_hash;
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    19
    char result[33];
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    20
    char byte[3];
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    21
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    22
    int i, j;
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    23
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    24
    if (argc != 2) {
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    25
        printf ("Usage: %s <text>\n", argv[0]);
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    26
        return EXIT_FAILURE;
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    27
    }
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    28
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    29
    /*
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    30
     * MD5 alters the input buffer
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    31
     */
162
fad55be2d07c damn strncpy function
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 148
diff changeset
    32
    strncpy (buffer, argv[1], sizeof (buffer) - 1);
fad55be2d07c damn strncpy function
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 148
diff changeset
    33
    strncpy (text, argv[1], sizeof (text) - 1);
45
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    34
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    35
    md5_hash = MD5 ((u_char *) buffer, strlen (buffer), NULL);
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    36
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    37
    for (i = 0, j = 0; i < 16; i++) {
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    38
        sprintf (byte, "%02x", (md5_hash[i] & 0xFF));
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    39
        result[j++] = byte[0];
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    40
        result[j++] = byte[1];
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    41
    }
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    42
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    43
    result[j] = 0;
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    44
    printf ("%s: %s\n", text, result);
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    45
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    46
    return EXIT_SUCCESS;
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
diff changeset
    47
}