File: uuid.cc

package info (click to toggle)
golang-github-google-certificate-transparency 0.0~git20160709.0.0f6e3d1~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,676 kB
  • sloc: cpp: 35,278; python: 11,838; java: 1,911; sh: 1,885; makefile: 950; xml: 520; ansic: 225
file content (43 lines) | stat: -rw-r--r-- 1,034 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iomanip>
#include <random>
#include <sstream>

#include "util/uuid.h"

using std::hex;
using std::mt19937;
using std::nouppercase;
using std::random_device;
using std::setw;
using std::setfill;
using std::string;
using std::stringstream;
using std::uniform_int_distribution;

namespace cert_trans {

string UUID4() {
  random_device rd;
  mt19937 twister(rd());
  uniform_int_distribution<uint32_t> distribution(0, UINT32_MAX);

  const uint32_t a((distribution(twister) & 0xFFFFFFFFUL));
  const uint32_t b((distribution(twister) & 0xFFFF0FFFUL) | 0x00004000UL);
  const uint32_t c((distribution(twister) & 0x3FFFFFFFUL) | 0x80000000UL);
  const uint32_t d((distribution(twister) & 0xFFFFFFFFUL));

  stringstream oss;
  oss << hex << nouppercase << setfill('0');

  oss << setw(8) << (a) << '-';
  oss << setw(4) << (b >> 16) << '-';
  oss << setw(4) << (b & 0xFFFF) << '-';
  oss << setw(4) << (c >> 16) << '-';
  oss << setw(4) << (c & 0xFFFF);
  oss << setw(8) << d;

  return oss.str();
}


}  // namespace cert_trans