2002-04-30 22:22:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "md5.h"
|
|
|
|
#include "md5_file.h"
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
int md5_file(char* path, char* output, double& nbytes) {
|
|
|
|
unsigned char buf[4096];
|
|
|
|
unsigned char binout[16];
|
|
|
|
FILE* f;
|
|
|
|
md5_state_t state;
|
|
|
|
int i, n;
|
2002-07-11 01:09:53 +00:00
|
|
|
if(path==NULL) {
|
|
|
|
fprintf(stderr, "error: md5_file: unexpected NULL pointer path\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
|
|
|
if(output==NULL) {
|
|
|
|
fprintf(stderr, "error: md5_file: unexpected NULL pointer output\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
nbytes = 0;
|
2002-06-19 18:37:08 +00:00
|
|
|
f = fopen(path, "rb");
|
2002-05-24 04:29:10 +00:00
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "md5_file: can't open %s\n", path);
|
|
|
|
perror("md5_file");
|
|
|
|
return -1;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
md5_init(&state);
|
|
|
|
while (1) {
|
|
|
|
n = fread(buf, 1, 4096, f);
|
|
|
|
if (n<=0) break;
|
|
|
|
nbytes += n;
|
|
|
|
md5_append(&state, buf, n);
|
|
|
|
}
|
|
|
|
md5_finish(&state, binout);
|
|
|
|
for (i=0; i<16; i++) {
|
|
|
|
sprintf(output+2*i, "%02x", binout[i]);
|
|
|
|
}
|
|
|
|
output[32] = 0;
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
|
|
|
|
int md5_block(unsigned char* data, int nbytes, char* output) {
|
|
|
|
unsigned char binout[16];
|
|
|
|
int i;
|
2002-07-11 01:09:53 +00:00
|
|
|
if(data==NULL) {
|
|
|
|
fprintf(stderr, "error: md5_block: unexpected NULL pointer data\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
|
|
|
if(nbytes<0) {
|
|
|
|
fprintf(stderr, "error: md5_block: negative nbytes\n");
|
|
|
|
return ERR_NEG;
|
|
|
|
}
|
|
|
|
if(output==NULL) {
|
|
|
|
fprintf(stderr, "error: md5_block: unexpected NULL pointer output\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
md5_state_t state;
|
|
|
|
md5_init(&state);
|
|
|
|
md5_append(&state, data, nbytes);
|
|
|
|
md5_finish(&state, binout);
|
|
|
|
for (i=0; i<16; i++) {
|
|
|
|
sprintf(output+2*i, "%02x", binout[i]);
|
|
|
|
}
|
|
|
|
output[32] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|