summaryrefslogtreecommitdiff
path: root/rfile.c
blob: 497d383bc531de5dad1409fd0fd18af76079f35c (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "prism.h"
#include "md5.h"
#include "rfile.h"

static const char hex_str[]= "0123456789abcdef";

int get_file_hash(const char *filename, char *hash)
{
FILE *fp;
MD5_CTX hb;
char buf[16];
int i;

    if(filename == NULL || hash == NULL)
        return PRET_ERROR;
        
    fp = fopen(filename, "rb");
    if(fp == NULL)
        return PRET_READERROR;
    
    MD5_Init(&hb);
    while(fgets(buf, 15, fp)) {
        MD5_Update(&hb, buf, strlen(buf));
    }
    fclose(fp);
    
    MD5_Final(buf, &hb);
    
    for(i=0; i<16; i++) {
        hash[2*i] = hex_str[buf[i] >> 4];
        hash[2*i+1] = hex_str[buf[i] & 0x0F];
    }
    
    return PRET_OK;
}