nearby: snapshot of cl/313536507

Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: I8936b527079074d5c3af5531b9245063767cc4a7
This commit is contained in:
Alexey Polyudov
2020-05-28 00:03:22 -07:00
parent 667bf4ee3b
commit ae1c427b99
334 changed files with 20315 additions and 1487 deletions
+22
View File
@@ -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())));