mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
utils: add some helpers to generate UUIDs
This commit is contained in:
parent
7e9583187b
commit
bf1285a71a
2 changed files with 33 additions and 0 deletions
|
@ -36,6 +36,8 @@
|
|||
#include <sys/types.h>
|
||||
|
||||
#include <openssl/sha.h>
|
||||
#include <jansson.h>
|
||||
#include <uuid/uuid.h>
|
||||
|
||||
#include <villas/config.h>
|
||||
#include <villas/log.h>
|
||||
|
@ -200,6 +202,12 @@ size_t strlenp(const char *str);
|
|||
*/
|
||||
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 {
|
||||
|
||||
using byte = std::uint8_t;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
/** Utilities.
|
||||
*
|
||||
* @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
|
||||
* @license GNU General Public License (version 3)
|
||||
*
|
||||
|
@ -38,6 +39,9 @@
|
|||
#include <openssl/bio.h>
|
||||
#include <openssl/buffer.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/md5.h>
|
||||
#include <jansson.h>
|
||||
#include <uuid/uuid.h>
|
||||
|
||||
#include <villas/config.h>
|
||||
#include <villas/utils.hpp>
|
||||
|
@ -353,5 +357,26 @@ int sha1sum(FILE *f, unsigned char *sha1)
|
|||
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 villas */
|
||||
|
|
Loading…
Add table
Reference in a new issue