#include "core/internal/mediums/utils.h" #include #include #include "platform/exception.h" #include "platform/prng.h" #include "absl/strings/escaping.h" namespace location { namespace nearby { namespace connections { void Utils::closeSocket(Ptr socket, const std::string& type, const std::string& name) { if (!socket.isNull()) { Exception::Value e = socket->close(); if (Exception::NONE != e) { if (Exception::IO == e) { // TODO(reznor): log.atWarning().withCause(e).log("Failed to close // %sSocket %s", type, name); } return; } // TODO(reznor): log.atVerbose().log("Closed %sSocket %s", type, name); } } ConstPtr Utils::sha256Hash(Ptr hash_utils, ConstPtr source, size_t length) { if (source.isNull()) { return ConstPtr(); } ScopedPtr> full_hash( hash_utils->sha256(std::string(source->getData(), source->size()))); return MakeConstPtr(new ByteArray(full_hash->getData(), length)); } ConstPtr Utils::legacySha256HashOnlyForPrinting( Ptr hash_utils, ConstPtr source, size_t length) { if (source.isNull()) { return ConstPtr(); } std::string formatted_hex_string = Utils::bytesToPrintableHexString(source); ScopedPtr> formatted_hex_byte_array(MakeConstPtr( new ByteArray(formatted_hex_string.data(), formatted_hex_string.size()))); return Utils::sha256Hash(hash_utils, formatted_hex_byte_array.get(), length); } ConstPtr 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 bytes) { std::string hex_string( absl::BytesToHexString(std::string(bytes->getData(), bytes->size()))); // Print out the byte array as a space separated listing of hex bytes. std::ostringstream formatted_hex_string_stream; formatted_hex_string_stream << "[ "; for (int i = 0; i < hex_string.size(); i += 2) { formatted_hex_string_stream << "0x"; // This is safe because we have the guarantee that hex_string is of even // length (because a hex encoding will always be double the size of its // input). formatted_hex_string_stream << hex_string[i] << hex_string[i + 1]; formatted_hex_string_stream << " "; } formatted_hex_string_stream << "]"; return formatted_hex_string_stream.str(); } } // namespace connections } // namespace nearby } // namespace location