|
| 1 | +// Copyright 2020 SkyAPM |
| 2 | + |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// From |
| 16 | +// https://github.com/envoyproxy/envoy/blob/master/source/common/common/random_generator.{h,cc} |
| 17 | + |
| 18 | +#include "source/utils/random_generator.h" |
| 19 | + |
| 20 | +namespace cpp2sky { |
| 21 | + |
| 22 | +std::string RandomGeneratorImpl::uuid() { |
| 23 | + static thread_local char buffered[2048]; |
| 24 | + static thread_local size_t buffered_idx = sizeof(buffered); |
| 25 | + |
| 26 | + if (buffered_idx + 16 > sizeof(buffered)) { |
| 27 | + // TODO(shikugawa): Self-implemented random number generator is used right |
| 28 | + // now. RAND_bytes on OpenSSL is used on Envoy's RandomGenerator. |
| 29 | + randomBuffer(buffered, sizeof(buffered)); |
| 30 | + buffered_idx = 0; |
| 31 | + } |
| 32 | + |
| 33 | + // Consume 16 bytes from the buffer. |
| 34 | + assert(buffered_idx + 16 <= sizeof(buffered)); |
| 35 | + char* rand = &buffered[buffered_idx]; |
| 36 | + buffered_idx += 16; |
| 37 | + |
| 38 | + // Create UUID from Truly Random or Pseudo-Random Numbers. |
| 39 | + // See: https://tools.ietf.org/html/rfc4122#section-4.4 |
| 40 | + rand[6] = (rand[6] & 0x0f) | 0x40; // UUID version 4 (random) |
| 41 | + rand[8] = (rand[8] & 0x3f) | 0x80; // UUID variant 1 (RFC4122) |
| 42 | + |
| 43 | + // Convert UUID to a string representation, e.g. |
| 44 | + // a121e9e1-feae-4136-9e0e-6fac343d56c9. |
| 45 | + static const char* const hex = "0123456789abcdef"; |
| 46 | + char uuid[UUID_LENGTH]; |
| 47 | + |
| 48 | + for (uint8_t i = 0; i < 4; i++) { |
| 49 | + const uint8_t d = rand[i]; |
| 50 | + uuid[2 * i] = hex[d >> 4]; |
| 51 | + uuid[2 * i + 1] = hex[d & 0x0f]; |
| 52 | + } |
| 53 | + |
| 54 | + uuid[8] = '-'; |
| 55 | + |
| 56 | + for (uint8_t i = 4; i < 6; i++) { |
| 57 | + const uint8_t d = rand[i]; |
| 58 | + uuid[2 * i + 1] = hex[d >> 4]; |
| 59 | + uuid[2 * i + 2] = hex[d & 0x0f]; |
| 60 | + } |
| 61 | + |
| 62 | + uuid[13] = '-'; |
| 63 | + |
| 64 | + for (uint8_t i = 6; i < 8; i++) { |
| 65 | + const uint8_t d = rand[i]; |
| 66 | + uuid[2 * i + 2] = hex[d >> 4]; |
| 67 | + uuid[2 * i + 3] = hex[d & 0x0f]; |
| 68 | + } |
| 69 | + |
| 70 | + uuid[18] = '-'; |
| 71 | + |
| 72 | + for (uint8_t i = 8; i < 10; i++) { |
| 73 | + const uint8_t d = rand[i]; |
| 74 | + uuid[2 * i + 3] = hex[d >> 4]; |
| 75 | + uuid[2 * i + 4] = hex[d & 0x0f]; |
| 76 | + } |
| 77 | + |
| 78 | + uuid[23] = '-'; |
| 79 | + |
| 80 | + for (uint8_t i = 10; i < 16; i++) { |
| 81 | + const uint8_t d = rand[i]; |
| 82 | + uuid[2 * i + 4] = hex[d >> 4]; |
| 83 | + uuid[2 * i + 5] = hex[d & 0x0f]; |
| 84 | + } |
| 85 | + |
| 86 | + return std::string(uuid, UUID_LENGTH); |
| 87 | +} |
| 88 | + |
| 89 | +void RandomGeneratorImpl::randomBuffer(char* ch, size_t len) { |
| 90 | + std::random_device engine; |
| 91 | + std::uniform_int_distribution<std::size_t> dist(0, CHARS.size() - 1); |
| 92 | + for (auto i = 0; i < len; ++i) { |
| 93 | + ch[i] = CHARS[dist(engine)]; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace cpp2sky |
0 commit comments