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 
26 #include <cstdio>
27 #include <openssl/evp.h>
28 
29 #define FILE_STEP 1024
30 
31 using 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 
40 /** Constructor
41  * @param config_filename file name of config (interface template)
42  */
43 InterfaceDigest::InterfaceDigest(std::string config_filename)
44 {
45  digest = NULL;
46 
47  EVP_MD_CTX *ctx = EVP_MD_CTX_create();
48  if ( ! EVP_DigestInit(ctx, EVP_md5())) {
49  EVP_MD_CTX_destroy(ctx);
50  throw Exception("Could not initialize digest context");
51  }
52 
53  FILE *f = fopen(config_filename.c_str(), "r");
54  void *buf = malloc(FILE_STEP);
55  while ( ! feof(f) && ! ferror(f) ) {
56  size_t rb;
57  if ((rb = fread(buf, 1, FILE_STEP, f)) > 0) {
58  if ( ! EVP_DigestUpdate(ctx, buf, rb) ) {
59  fclose(f);
60  EVP_MD_CTX_destroy(ctx);
61  throw Exception("Failed to update digest");
62  }
63  }
64  }
65  if ( ferror(f) ) {
66  fclose(f);
67  EVP_MD_CTX_destroy(ctx);
68  throw Exception("Failure while reading the file");
69  }
70  fclose(f);
71 
72  digest_size=EVP_MD_CTX_size(ctx);
73  digest = new unsigned char[digest_size];
74 
75  if ( ! EVP_DigestFinal(ctx, digest, NULL) ) {
76  delete digest;
77  digest = NULL;
78  EVP_MD_CTX_destroy(ctx);
79  throw Exception("Could not finalize digest");
80  }
81  EVP_MD_CTX_destroy(ctx);
82 }
83 
84 
85 /** Destructor. */
87 {
88  delete digest;
89 }
90 
91 
92 /** Get hash.
93  * @return hash
94  */
95 const unsigned char *
97 {
98  return digest;
99 }
100 
101 
102 /** Get hash size.
103  * @return hash size
104  */
105 size_t
107 {
108  return digest_size;
109 }
Fawkes library namespace.
const unsigned char * get_hash()
Get hash.
Definition: digest.cpp:96
Base class for exceptions in Fawkes.
Definition: exception.h:36
size_t get_hash_size()
Get hash size.
Definition: digest.cpp:106
~InterfaceDigest()
Destructor.
Definition: digest.cpp:86
InterfaceDigest(std::string config_filename)
Constructor.
Definition: digest.cpp:43