Fawkes API Fawkes Development Version
digest.cpp
1
2/***************************************************************************
3 * digest.cpp - Interface config parser
4 *
5 * Created: Thu Feb 28 15:51:20 2008
6 * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23#include <interfaces/generator/digest.h>
24#include <interfaces/generator/exceptions.h>
25#include <openssl/evp.h>
26
27#include <cstdio>
28
29#define FILE_STEP 1024
30
31using namespace fawkes;
32
33/** @class InterfaceDigest <interfaces/generator/digest.h>
34 * Interface digest generator.
35 * Creates the MD5 hash of the given config file.
36 * @author Tim Niemueller
37 */
38
39/** Constructor
40 * @param config_filename file name of config (interface template)
41 */
42InterfaceDigest::InterfaceDigest(std::string config_filename)
43{
44 digest = NULL;
45
46 EVP_MD_CTX *ctx = EVP_MD_CTX_create();
47 if (!EVP_DigestInit(ctx, EVP_md5())) {
48 EVP_MD_CTX_destroy(ctx);
49 throw Exception("Could not initialize digest context");
50 }
51
52 FILE *f = fopen(config_filename.c_str(), "r");
53 void *buf = malloc(FILE_STEP);
54 while (!feof(f) && !ferror(f)) {
55 size_t rb;
56 if ((rb = fread(buf, 1, FILE_STEP, f)) > 0) {
57 if (!EVP_DigestUpdate(ctx, buf, rb)) {
58 fclose(f);
59 EVP_MD_CTX_destroy(ctx);
60 throw Exception("Failed to update digest");
61 }
62 }
63 }
64 if (ferror(f)) {
65 fclose(f);
66 EVP_MD_CTX_destroy(ctx);
67 throw Exception("Failure while reading the file");
68 }
69 fclose(f);
70
71 digest_size = EVP_MD_CTX_size(ctx);
72 digest = new unsigned char[digest_size];
73
74 if (!EVP_DigestFinal(ctx, digest, NULL)) {
75 delete[] digest;
76 digest = NULL;
77 EVP_MD_CTX_destroy(ctx);
78 throw Exception("Could not finalize digest");
79 }
80 EVP_MD_CTX_destroy(ctx);
81}
82
83/** Destructor. */
85{
86 delete[] digest;
87}
88
89/** Get hash.
90 * @return hash
91 */
92const unsigned char *
94{
95 return digest;
96}
97
98/** Get hash size.
99 * @return hash size
100 */
101size_t
103{
104 return digest_size;
105}
~InterfaceDigest()
Destructor.
Definition: digest.cpp:84
const unsigned char * get_hash()
Get hash.
Definition: digest.cpp:93
InterfaceDigest(std::string config_filename)
Constructor.
Definition: digest.cpp:42
size_t get_hash_size()
Get hash size.
Definition: digest.cpp:102
Base class for exceptions in Fawkes.
Definition: exception.h:36
Fawkes library namespace.