Aufgrund einer Wartung wird GitLab am 25.01 zwischen 8:00 und 9:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 25.01 between 8:00 and 9:00 am.

Commit 2439e56d authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

utils: add some helpers to generate UUIDs

parent dfd8bb23
...@@ -36,6 +36,8 @@ ...@@ -36,6 +36,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <jansson.h>
#include <uuid/uuid.h>
#include <villas/config.h> #include <villas/config.h>
#include <villas/log.h> #include <villas/log.h>
...@@ -200,6 +202,12 @@ size_t strlenp(const char *str); ...@@ -200,6 +202,12 @@ size_t strlenp(const char *str);
*/ */
int sha1sum(FILE *f, unsigned char *sha1); int sha1sum(FILE *f, unsigned char *sha1);
/** Generate an UUID by MD5 hashing the provided string */
void uuid_generate_from_str(uuid_t out, const std::string &data, const std::string &ns = "");
/** Generate an UUID by MD5 hashing the serialized representation of the provided JSON object */
void uuid_generate_from_json(uuid_t out, json_t *json, const std::string &ns = "");
namespace base64 { namespace base64 {
using byte = std::uint8_t; using byte = std::uint8_t;
......
/** Utilities. /** Utilities.
* *
* @author Daniel Krebs <github@daniel-krebs.net> * @author Daniel Krebs <github@daniel-krebs.net>
* @author Steffen Vogel <svogel2@eonerc.rwth-aachen.de>
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC * @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3) * @license GNU General Public License (version 3)
* *
...@@ -38,6 +39,9 @@ ...@@ -38,6 +39,9 @@
#include <openssl/bio.h> #include <openssl/bio.h>
#include <openssl/buffer.h> #include <openssl/buffer.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/md5.h>
#include <jansson.h>
#include <uuid/uuid.h>
#include <villas/config.h> #include <villas/config.h>
#include <villas/utils.hpp> #include <villas/utils.hpp>
...@@ -353,5 +357,26 @@ int sha1sum(FILE *f, unsigned char *sha1) ...@@ -353,5 +357,26 @@ int sha1sum(FILE *f, unsigned char *sha1)
return 0; return 0;
} }
void uuid_generate_from_str(uuid_t out, const std::string &data, const std::string &ns)
{
std::stringstream ss;
if (!ns.empty())
ss << ns << "|";
ss << data;
MD5((unsigned char*) ss.str().c_str(), ss.str().size(), (unsigned char *) &out);
}
void uuid_generate_from_json(uuid_t out, json_t *json, const std::string &ns)
{
char *str = json_dumps(json, JSON_COMPACT | JSON_SORT_KEYS);
uuid_generate_from_str(out, str, ns);
free(str);
}
} /* namespace utils */ } /* namespace utils */
} /* namespace villas */ } /* namespace villas */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment