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;
48  if ( ! EVP_DigestInit(&ctx, EVP_md5())) {
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  throw Exception("Failed to update digest");
60  }
61  }
62  }
63  if ( ferror(f) ) {
64  fclose(f);
65  throw Exception("Failure while reading the file");
66  }
67  fclose(f);
68 
69  digest_size=EVP_MD_CTX_size(&ctx);
70  digest = new unsigned char[digest_size];
71 
72  if ( ! EVP_DigestFinal(&ctx, digest, NULL) ) {
73  delete digest;
74  digest = NULL;
75  throw Exception("Could not finalize digest");
76  }
77 }
78 
79 
80 /** Destructor. */
82 {
83  delete digest;
84 }
85 
86 
87 /** Get hash.
88  * @return hash
89  */
90 const unsigned char *
92 {
93  return digest;
94 }
95 
96 
97 /** Get hash size.
98  * @return hash size
99  */
100 size_t
102 {
103  return digest_size;
104 }
Fawkes library namespace.
const unsigned char * get_hash()
Get hash.
Definition: digest.cpp:91
Base class for exceptions in Fawkes.
Definition: exception.h:36
size_t get_hash_size()
Get hash size.
Definition: digest.cpp:101
~InterfaceDigest()
Destructor.
Definition: digest.cpp:81
InterfaceDigest(std::string config_filename)
Constructor.
Definition: digest.cpp:43