mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
nearby: snapshot of cl/313536507
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I8936b527079074d5c3af5531b9245063767cc4a7
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
#include "core/internal/mediums/utils.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
#include "platform/exception.h"
|
||||
#include "platform/prng.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
|
||||
namespace location {
|
||||
@@ -48,6 +50,26 @@ ConstPtr<ByteArray> Utils::legacySha256HashOnlyForPrinting(
|
||||
return Utils::sha256Hash(hash_utils, formatted_hex_byte_array.get(), length);
|
||||
}
|
||||
|
||||
ConstPtr<ByteArray> Utils::generateRandomBytes(size_t length) {
|
||||
Prng rng;
|
||||
std::string data;
|
||||
data.reserve(length);
|
||||
|
||||
// Adds 4 random bytes per iteration.
|
||||
while (length > 0) {
|
||||
std::uint32_t val = rng.nextUInt32();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
data += val & 0xFF;
|
||||
val >>= 8;
|
||||
length--;
|
||||
|
||||
if (!length) break;
|
||||
}
|
||||
}
|
||||
|
||||
return MakeConstPtr(new ByteArray(data));
|
||||
}
|
||||
|
||||
std::string Utils::bytesToPrintableHexString(ConstPtr<ByteArray> bytes) {
|
||||
std::string hex_string(
|
||||
absl::BytesToHexString(std::string(bytes->getData(), bytes->size())));
|
||||
|
||||
Reference in New Issue
Block a user