Added implementation of gnudip protocol.
[bpdyndnsd] / src / util.cpp
1 /** @file
2  * @brief Util class implementation.
3  *
4  *
5  *
6  * @copyright Intra2net AG
7  * @license GPLv2
8 */
9
10 #include "util.h"
11
12 #include <stdio.h>
13 #include <openssl/evp.h>
14 #include <openssl/md5.h>
15
16 Util::Util()
17 {
18 }
19
20 Util::~Util()
21 {
22 }
23
24 /**
25  * Computes a MD5 Digest from the given string and returns the HEX representation
26  * @param data The string to compute the md5 for
27  * @return The computed md5 in hex
28  */
29 std::string Util::compute_md5_digest(std::string data)
30 {
31     // compute an MD5 digest.
32
33     string result;
34     EVP_MD_CTX mdctx;
35     const EVP_MD *md;
36     unsigned char md_value[EVP_MAX_MD_SIZE];
37     unsigned int md_len;
38
39     OpenSSL_add_all_digests();
40
41     md = EVP_get_digestbyname("md5");
42
43     EVP_MD_CTX_init(&mdctx);
44     EVP_DigestInit_ex(&mdctx, md, NULL);
45     EVP_DigestUpdate(&mdctx, data.c_str(), strlen(data.c_str()));
46     EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
47     EVP_MD_CTX_cleanup(&mdctx);
48
49     char buffer[2];
50
51     for(int i = 0; i < md_len; i++)
52     {
53         sprintf(buffer,"%02x", md_value[i]);
54         result.append(buffer);
55     }
56
57     return result;
58 }