2003-06-11 23:36:48 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-09-26 18:11:06 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-06-11 23:36:48 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2004-07-13 13:54:09 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2004-07-13 13:54:09 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
2004-07-03 21:38:15 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
#include "fcgi_stdio.h"
|
|
|
|
#endif
|
|
|
|
|
2004-07-13 14:18:47 +00:00
|
|
|
int md5_file(const char* path, char* output, double& nbytes) {
|
2002-04-30 22:22:54 +00:00
|
|
|
unsigned char buf[4096];
|
|
|
|
unsigned char binout[16];
|
|
|
|
FILE* f;
|
|
|
|
md5_state_t state;
|
|
|
|
int i, n;
|
2002-08-30 20:56:02 +00:00
|
|
|
|
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) {
|
2002-10-03 18:33:46 +00:00
|
|
|
fprintf(stderr, "md5_file: can't open %s\n", path);
|
2002-05-24 04:29:10 +00:00
|
|
|
perror("md5_file");
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_FOPEN;
|
2002-05-24 04:29:10 +00:00
|
|
|
}
|
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
|
|
|
|
2004-07-13 14:18:47 +00:00
|
|
|
int md5_block(const unsigned char* data, int nbytes, char* output) {
|
2002-07-05 05:33:40 +00:00
|
|
|
unsigned char binout[16];
|
|
|
|
int i;
|
2002-08-30 20:56:02 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2004-07-13 14:18:47 +00:00
|
|
|
|
|
|
|
std::string md5_string(const unsigned char* data, int nbytes)
|
|
|
|
{
|
2004-07-20 10:38:14 +00:00
|
|
|
char output[32];
|
2004-07-13 14:18:47 +00:00
|
|
|
md5_block(data, nbytes, output);
|
2004-07-20 10:38:14 +00:00
|
|
|
return std::string(output);
|
2004-07-13 14:18:47 +00:00
|
|
|
}
|
|
|
|
|
2004-12-08 00:40:19 +00:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
static volatile const char __attribute__((unused)) *BOINCrcsid="$Id$";
|
|
|
|
#else
|
|
|
|
static volatile const char *BOINCrcsid="$Id$";
|
|
|
|
#endif
|