32 lines
873 B
C++
32 lines
873 B
C++
#include "uuid.h"
|
|
#include <random>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
std::string UUID::generate_uuid() {
|
|
std::stringstream hexstream;
|
|
hexstream << UUID::generate_hex(4) << "-" << UUID::generate_hex(2) << "-"
|
|
<< UUID::generate_hex(2) << "-" << UUID::generate_hex(2) << "-"
|
|
<< UUID::generate_hex(6);
|
|
return hexstream.str();
|
|
}
|
|
|
|
std::string UUID::generate_hex(const unsigned int len) {
|
|
std::stringstream ss;
|
|
for (auto i = 0; i < len; i++) {
|
|
const auto rc = random_char();
|
|
std::stringstream hexstream;
|
|
hexstream << std::hex << rc;
|
|
auto hex = hexstream.str();
|
|
ss << (hex.length() < 2 ? '0' + hex : hex);
|
|
}
|
|
return ss.str();
|
|
}
|
|
|
|
unsigned int UUID::random_char() {
|
|
std::random_device rd;
|
|
std::mt19937 gen(rd());
|
|
std::uniform_int_distribution<> dis(0, 255);
|
|
return dis(gen);
|
|
}
|