// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "core/internal/mediums/utils.h" #include #include "platform/exception.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); } 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