/usr/include/kashmir/uuid_gen.h is in libkashmir-dev 0.0~git20150805.0.2f3913f+dfsg3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | // uuid_gen.h -- universally unique ID - as defined by ISO/IEC 9834-8:2005
// Copyright (C) 2008 Kenneth Laskoski
/** @file uuid_gen.h
@brief universally unique ID - as defined by ISO/IEC 9834-8:2005
@author Copyright (C) 2008 Kenneth Laskoski
*/
#ifndef KL_UUID_GEN_H
#define KL_UUID_GEN_H
#include "uuid.h"
#include "md5_gen.h"
#include "sha1_gen.h"
#include "randomstream.h"
namespace kashmir {
namespace uuid {
template<class crtp_impl>
randomstream<crtp_impl>& operator>>(randomstream<crtp_impl>& is, uuid_t& uuid)
{
// get random bytes
// we take advantage of our representation
char *data = reinterpret_cast<char*>(&uuid);
is.read(data, size);
// a more general solution would be
// input_iterator<is> it;
// std::copy(it, it+size, data.begin());
// if uuid_t::value_type is larger than 8 bits, we need
// to maintain the invariant data[i] < 256 for i < 16
// Example (which may impact randomness):
// for (size_t i = 0; i < size; ++i)
// data[i] &= 0xff;
// set variant
// should be 0b10xxxxxx
data[8] &= 0xbf; // 0b10111111
data[8] |= 0x80; // 0b10000000
// set version
// should be 0b0100xxxx
data[6] &= 0x4f; // 0b01001111
data[6] |= 0x40; // 0b01000000
return is;
}
template<class crtp_impl>
uuid_t generate(randomstream<crtp_impl>& is)
{
uuid_t uuid;
is >> uuid;
return uuid;
}
template<class crtp_impl>
uuid_t generate(md5::engine<crtp_impl>& md5engine, const uuid_t& nameSpace, const std::string& name)
{
md5engine.update(reinterpret_cast<const char *const>(&nameSpace), 16);
md5engine.update(name.c_str(), name.size());
kashmir::md5_t md5 = md5engine();
kashmir::uuid_t& uuid = *(reinterpret_cast<kashmir::uuid_t*>(&md5));
unsigned char *const data = reinterpret_cast<unsigned char *const>(&uuid);
// set variant
// should be 0b10xxxxxx
data[8] &= 0xbf; // 0b10111111
data[8] |= 0x80; // 0b10000000
// set version
// should be 0b0011xxxx
data[6] &= 0x3f; // 0b00111111
data[6] |= 0x30; // 0b00110000
return uuid;
}
template<class crtp_impl>
uuid_t generate(sha1::engine<crtp_impl>& sha1engine, const uuid_t& nameSpace, const std::string& name)
{
sha1engine.update(reinterpret_cast<const char *const>(&nameSpace), 16);
sha1engine.update(name.c_str(), name.size());
kashmir::sha1_t sha1 = sha1engine();
kashmir::uuid_t& uuid = *(reinterpret_cast<kashmir::uuid_t*>(&sha1));
unsigned char *const data = reinterpret_cast<unsigned char *const>(&uuid);
// set variant
// should be 0b10xxxxxx
data[8] &= 0xbf; // 0b10111111
data[8] |= 0x80; // 0b10000000
// set version
// should be 0b0101xxxx
data[6] &= 0x5f; // 0b01011111
data[6] |= 0x50; // 0b01010000
return uuid;
}
} // namespace kashmir::uuid
} // namespace kashmir
#endif
|