scclib
Stable Cloud Computing C++ Library
uuid.cc
1 
2 /*
3 BSD 3-Clause License
4 
5 Copyright (c) 2022, Stable Cloud Computing, Inc.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its
18  contributors may be used to endorse or promote products derived from
19  this software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 #include <crypto/uuid.h>
33 #include <ctype.h>
34 #include <algorithm>
35 #include <vector>
36 #include <string>
37 #include <sstream>
38 #include <crypto/random.h>
39 #include <encode/hex.h>
40 
41 using namespace scc::crypto;
42 using scc::encode::Hex;
43 
44 std::ostream& operator <<(std::ostream& os, const Uuid& u)
45 {
46  return os.write(u.val().c_str(), u.val().size());
47 }
48 void scc::crypto::PrintTo(const Uuid& u, std::ostream* os)
49 {
50  *os << u;
51 }
52 
53 const std::string Uuid::zero = "00000000-0000-0000-0000-000000000000";
54 
55 void Uuid::assign(const std::string& s)
56 {
57  std::stringstream str(s);
58  std::string tok;
59  std::vector<std::string> v;
60 
61  while (std::getline(str, tok, '-'))
62  {
63  v.push_back(tok);
64  }
65 
66  if (v.size() != 5) { m_uuid = zero; return; }
67 
68  if (v[0].size() != 8) { m_uuid = zero; return; }
69  if (v[1].size() != 4) { m_uuid = zero; return; }
70  if (v[2].size() != 4) { m_uuid = zero; return; }
71  if (v[3].size() != 4) { m_uuid = zero; return; }
72  if (v[4].size() != 12) { m_uuid = zero; return; }
73 
74  for (auto& x : v)
75  {
76  for (auto b : x)
77  {
78  if ( ((b<'0')||(b>'9')) && ((b<'a')||(b>'f')) && ((b<'A')||(b>'F')) )
79  {
80  m_uuid = zero; return;
81  }
82  }
83  }
84 
85  // NOTE: does not check for binary requirements
86 
87  m_uuid = s;
88 
89  std::for_each(m_uuid.begin(), m_uuid.end(), [](char& c)
90  {
91  c = tolower(c);
92  });
93 }
94 
95 std::string Uuid::generate()
96 {
97  std::vector<char> u(16);
98  RandomEngine::rand_bytes(&u[0], 16);
99 
100  // set most significant bits of time_hi_and_version (octet 6) to 0100 (version)
101  u[6] &= 0x4f;
102  u[6] |= 0x40;
103 
104  // set most significant bits of clock_seq_hi_and_reserved (octet 8) to 01
105  u[8] &= 0x7f;
106  u[8] |= 0x40;
107 
108  // emit hex HxHxHxHx-HxHx-HxHx-HxHx-HxHxHxHxHxHx
109  std::stringstream uuid;
110 
111  typedef std::vector<char> CharVec;
112 
113  uuid << Hex::bin_to_hex(CharVec(&u[0], &u[4]));
114  uuid << "-";
115  uuid << Hex::bin_to_hex(CharVec(&u[4], &u[6]));
116  uuid << "-";
117  uuid << Hex::bin_to_hex(CharVec(&u[6], &u[8]));
118  uuid << "-";
119  uuid << Hex::bin_to_hex(CharVec(&u[8], &u[10]));
120  uuid << "-";
121  uuid << Hex::bin_to_hex(CharVec(&u[10], &u[16]));
122 
123  m_uuid = uuid.str();
124 
125  return m_uuid;
126 }
void PrintTo(const Bignum &, std::ostream *)
Googletest printer.
static void rand_bytes(void *, int len)
Generate random bytes.
Universally unique identifier (uuid).
Definition: uuid.h:63
std::string val() const
Return the uuid value.
Definition: uuid.h:117
static const std::string zero
Zero uuid is 00000000-0000-0000-000000000000.
Definition: uuid.h:131
std::string generate()
Generate a new uuid.
Definition: uuid.cc:95
Binary to hex string converter.
std::ostream & operator<<(std::ostream &, const scc::net::InetAddr &)
Print the socket address details to an output stream.
Definition: inet.cc:320
Random number generator.
Universally Unique Identifier (uuid).