/* * Copyright 2004 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree.
*/
// Lock for the global random generator, only needed to serialize changing the // generator.
webrtc::Mutex& GetRandomGeneratorLock() { static webrtc::Mutex& mutex = *new webrtc::Mutex(); return mutex;
}
// This round about way of creating a global RNG is to safe-guard against // indeterminant static initialization order.
std::unique_ptr<RandomGenerator>& GetGlobalRng() { static std::unique_ptr<RandomGenerator>& global_rng =
*new std::unique_ptr<RandomGenerator>(new SecureRandomGenerator());
bool CreateRandomData(size_t length, std::string* data) {
data->resize(length); // std::string is guaranteed to use contiguous memory in c++11 so we can // safely write directly to it. return Rng().Generate(&data->at(0), length);
}
// Version 4 UUID is of the form: // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx // Where 'x' is a hex digit, and 'y' is 8, 9, a or b.
std::string CreateRandomUuid() {
std::string str;
std::unique_ptr<uint8_t[]> bytes(new uint8_t[31]);
RTC_CHECK(Rng().Generate(bytes.get(), 31));
str.reserve(36); for (size_t i = 0; i < 8; ++i) {
str.push_back(kHex[bytes[i] % 16]);
}
str.push_back('-'); for (size_t i = 8; i < 12; ++i) {
str.push_back(kHex[bytes[i] % 16]);
}
str.push_back('-');
str.push_back('4'); for (size_t i = 12; i < 15; ++i) {
str.push_back(kHex[bytes[i] % 16]);
}
str.push_back('-');
str.push_back(kUuidDigit17[bytes[15] % 4]); for (size_t i = 16; i < 19; ++i) {
str.push_back(kHex[bytes[i] % 16]);
}
str.push_back('-'); for (size_t i = 19; i < 31; ++i) {
str.push_back(kHex[bytes[i] % 16]);
} return str;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.