lsflib/src/md5sum.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:02 +0100
changeset 9 c3fecc82ade6
parent 6 c3dc3eb3b541
child 77 49e0babccb23
permissions -rw-r--r--
standard tags for git projects committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
     1
/**
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
     2
 * test/demos/lsflib/src/md5sum.c
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
6
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <string.h>
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <openssl/evp.h>
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <lsf.h>
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
unsigned char *md5sum (char *fname)
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
{
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
    EVP_MD_CTX mdctx;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    const EVP_MD *md;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
    char line[81];
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    unsigned char md_value[EVP_MAX_MD_SIZE];
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    int md_len, i;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
    FILE *f;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    static unsigned char *value = NULL;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    OpenSSL_add_all_digests ();
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    if ((f = fopen (fname, "rb")) == NULL) {
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
        perror ("FOPEN");
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
        value = malloc (1);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
        value = 0;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
        return value;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    }
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    if ((md = EVP_get_digestbyname ("md5")) == NULL) {
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        value = malloc (1);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        value = 0;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
        return value;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
    }
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    EVP_MD_CTX_init (&mdctx);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    EVP_DigestInit_ex (&mdctx, md, NULL);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    *line = 0;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
    while ((i = fread (line, 1, 80, f)) != 0) {
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        EVP_DigestUpdate (&mdctx, line, i);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    }
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
    fclose (f);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
    EVP_DigestFinal_ex (&mdctx, md_value, (unsigned int *)&md_len);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
    EVP_MD_CTX_cleanup (&mdctx);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
    value = malloc (md_len + 1);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
    for (i = 0; i < md_len; i++)
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
        value[i] = md_value[i];
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    value[i] = 0;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
    return value;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
}