2015-04-10 23:48:41 +02:00
|
|
|
/*
|
|
|
|
This file is part of libcapwap.
|
|
|
|
|
|
|
|
libcapwap is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
libcapwap is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief
|
2018-03-11 00:56:41 +01:00
|
|
|
*/
|
2015-04-06 14:32:20 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nettle/md5.h>
|
|
|
|
|
|
|
|
#include "capwap_crypto.h"
|
|
|
|
|
|
|
|
#define BLOCK_SIZE 4096
|
|
|
|
|
2015-04-07 14:54:26 +02:00
|
|
|
/**
|
2018-03-11 00:56:41 +01:00
|
|
|
* Calculate MD5 checksum for an opened file.
|
|
|
|
* @param digest destination buffer for calculated
|
2015-04-07 14:54:26 +02:00
|
|
|
* checksum (16 bytes, for a predifned constant use #CW_MD5_DIGEST_SIZE)
|
2018-03-11 00:56:41 +01:00
|
|
|
* @param infile file hanle
|
|
|
|
*
|
2015-04-07 14:54:26 +02:00
|
|
|
* Remember to set the file pointer to the beginning of the file, before
|
2015-04-10 23:48:41 +02:00
|
|
|
* calling this function. Therefore use fseek(file,0,SEEK_SET).
|
2015-04-07 14:54:26 +02:00
|
|
|
*/
|
2018-03-11 00:56:41 +01:00
|
|
|
int cw_fgetmd5sum ( uint8_t *digest, FILE *infile )
|
2015-04-06 14:32:20 +02:00
|
|
|
{
|
|
|
|
struct md5_ctx ctx;
|
|
|
|
uint8_t buffer[BLOCK_SIZE];
|
2018-03-11 00:56:41 +01:00
|
|
|
md5_init ( &ctx );
|
2015-04-06 14:32:20 +02:00
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
while ( !feof ( infile ) ) {
|
|
|
|
int bytes = fread ( buffer, 1, sizeof ( buffer ), infile );
|
|
|
|
md5_update ( &ctx, bytes, buffer );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ferror ( infile ) )
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
md5_digest ( &ctx, MD5_DIGEST_SIZE, digest );
|
2015-04-06 14:32:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|