[cr148] base::HexEncode to use span

The deprecated variant has been deleted/

Chromium changes:
https://chromium.googlesource.com/chromium/src/+/da12fb560afc4519dc2ae1f831714008fda90bac

commit da12fb560afc4519dc2ae1f831714008fda90bac
Author: raorui <ruirao565@gmail.com>
Date:   Fri Mar 27 03:27:18 2026 -0700

    Code Health: Use span in base::HexEncode

    - Migrate HexEncode callers from raw pointer+length to controlled views:
      - strings: std::string_view or base::as_byte_span
      - IOBuffer: io_buffer()->span()
      - struct bytes: reinterpret_cast + base::span<const uint8_t>
    - Clamp adapter MAC length before slicing to avoid OOB when encoding
    - Remove pointer-based HexEncode overload and associated TODO
    - Update tests and fuzzers to new interfaces

    Bug: 40284755
    Change-Id: I3352bb1a795b2f10cf5a8bedb432fe7d4bdf7290
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7607319
    Reviewed-by: Ken Buchanan <kenrb@chromium.org>
    Owners-Override: Colin Blundell <blundell@chromium.org>
    Commit-Queue: Colin Blundell <blundell@chromium.org>
    Reviewed-by: Colin Blundell <blundell@chromium.org>
    Cr-Commit-Position: refs/heads/main@{#1606068}
This commit is contained in:
Claudio DeSouza
2026-04-27 12:00:54 +01:00
parent d993a24c20
commit 4444291d91
14 changed files with 28 additions and 37 deletions
@@ -60,8 +60,7 @@ std::string GetPublisherChannelResponse(
bool use_alternate_publisher_list) {
std::string key;
for (const auto& pair : prefix_map) {
std::string hex = base::ToLowerASCII(
base::HexEncode(pair.first.data(), pair.first.size()));
std::string hex = base::HexEncodeLower(pair.first);
if (hex.find(prefix) == 0) {
key = pair.second;
break;
@@ -212,7 +212,7 @@ void BraveSyncHandler::HandleGetQRCode(const base::ListValue& args) {
// QR code version 3 can only carry 84 bytes so we hex encode 32 bytes
// seed then we will have 64 bytes input data
const std::string sync_code_hex = base::HexEncode(seed.data(), seed.size());
const std::string sync_code_hex = base::HexEncode(seed);
const std::string qr_code_string =
brave_sync::QrCodeData::CreateWithActualDate(sync_code_hex)->ToJson();
@@ -13,7 +13,7 @@ namespace brave_ads {
// WARNING: don't change these numbers. They are provided by the variations
// service, so will need the same values to match the enums
enum class UserActivityEventType : int8_t {
enum class UserActivityEventType : uint8_t {
/*00*/ kInitializedAds = 0,
/*01*/ kBrowserDidEnterForeground,
/*02*/ kBrowserDidEnterBackground,
@@ -38,10 +38,11 @@ void LogEvent(UserActivityEventType event_type) {
UserActivityManager::GetInstance().GetHistoryForTimeWindow(
kUserActivityTimeWindow.Get());
BLOG(6, "Triggered event: " << base::HexEncode(&event_type, sizeof(int8_t))
<< " (" << GetUserActivityScore(triggers, events)
<< ":" << kUserActivityThreshold.Get() << ":"
<< kUserActivityTimeWindow.Get() << ")");
BLOG(6, "Triggered event: "
<< base::HexEncode(base::byte_span_from_ref(event_type)) << " ("
<< GetUserActivityScore(triggers, events) << ":"
<< kUserActivityThreshold.Get() << ":"
<< kUserActivityTimeWindow.Get() << ")");
}
} // namespace
@@ -40,7 +40,7 @@ std::string EncodeEvents(const UserActivityEventList& events) {
[](const auto& event) { return event.type; });
const std::string encoded_eligible_events =
base::HexEncode(eligible_events.data(), eligible_events.size());
base::HexEncode(base::as_byte_span(eligible_events));
return base::ToUpperASCII(encoded_eligible_events);
}
@@ -23,7 +23,7 @@ std::string GetHashPrefixRaw(const std::string& publisher_key,
std::string GetHashPrefixInHex(const std::string& publisher_key,
size_t prefix_size) {
const std::string raw = GetHashPrefixRaw(publisher_key, prefix_size);
return base::HexEncode(raw.data(), raw.size());
return base::HexEncode(raw);
}
} // namespace brave_rewards::internal::publisher
@@ -34,7 +34,7 @@ namespace brave_rewards::internal::util {
std::string GenerateRandomHexString() {
uint8_t bytes[kLengthHexString];
crypto::RandBytes(bytes);
return base::HexEncode(bytes, sizeof(bytes));
return base::HexEncode(base::as_byte_span(bytes));
}
std::string GeneratePKCECodeVerifier() {
@@ -67,8 +67,7 @@ void ConnectBitFlyerWallet::Authorize(ConnectExternalWalletCallback callback) {
const std::string hashed_payment_id =
crypto::SHA256HashString(rewards_wallet->payment_id);
const std::string external_account_id =
base::HexEncode(hashed_payment_id.data(), hashed_payment_id.size());
const std::string external_account_id = base::HexEncode(hashed_payment_id);
bitflyer_server_.post_oauth().Request(
external_account_id, oauth_info_.code, oauth_info_.code_verifier,
@@ -9,7 +9,6 @@
#include <string>
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "crypto/random.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -136,13 +135,11 @@ TEST(CryptoTest, Ed25519KeyDerivation) {
std::vector<uint8_t> info = {0};
DeriveSigningKeysFromSeed(seed, &HKDF_SALT, &info, &public_key, &private_key);
EXPECT_EQ("f58ca446f0c33ee7e8e9874466da442b2e764afd77ad46034bdff9e01f9b87d4",
base::ToLowerASCII(
base::HexEncode(public_key.data(), public_key.size())));
base::HexEncodeLower(public_key));
EXPECT_EQ(
"b5abda6940984c5153a2ba3653f047f98dfb19e39c3e02f07c8bbb0bd8e8872ef58ca446"
"f0c33ee7e8e9874466da442b2e764afd77ad46034bdff9e01f9b87d4",
base::ToLowerASCII(
base::HexEncode(private_key.data(), private_key.size())));
base::HexEncodeLower(private_key));
std::vector<uint8_t> message(128);
::crypto::RandBytes(message);
@@ -212,8 +209,7 @@ TEST(CryptoTest, EncryptAndDecrypt) {
::crypto::RandBytes(message);
EXPECT_TRUE(Encrypt(message, nonce, key, &ciphertext));
EXPECT_TRUE(Decrypt(ciphertext, nonce, key, &out_message));
EXPECT_EQ(base::HexEncode(message.data(), message.size()),
base::HexEncode(out_message.data(), out_message.size()));
EXPECT_EQ(base::HexEncode(message), base::HexEncode(out_message));
}
TEST(CryptoTest, Passphrase) {
@@ -224,8 +220,7 @@ TEST(CryptoTest, Passphrase) {
EXPECT_TRUE(!passphrase.empty());
std::vector<uint8_t> to_bytes;
EXPECT_TRUE(PassphraseToBytes32(passphrase, &to_bytes));
EXPECT_EQ(base::HexEncode(bytes.data(), bytes.size()),
base::HexEncode(to_bytes.data(), to_bytes.size()));
EXPECT_EQ(base::HexEncode(bytes), base::HexEncode(to_bytes));
// original passphrase can be recovered
std::vector<uint8_t> bip_bytes;
@@ -261,7 +261,7 @@ DecodedZCashTransparentAddress& DecodedZCashTransparentAddress::operator=(
base::DictValue OrchardOutput::ToValue() const {
base::DictValue dict;
dict.Set("address", base::HexEncode(addr.data(), addr.size()));
dict.Set("address", base::HexEncode(addr));
dict.Set("amount", base::NumberToString(value));
if (memo) {
dict.Set("memo", base::HexEncode(memo.value()));
@@ -75,8 +75,7 @@ void BraveSyncAuthManager::RequestAccessToken() {
SyncAccountInfo BraveSyncAuthManager::DetermineAccountToUse(
const signin::IdentityManager* identity_manager) const {
if (!public_key_.empty()) {
const std::string client_id =
base::HexEncode(public_key_.data(), public_key_.size());
const std::string client_id = base::HexEncode(public_key_);
AccountInfo account_info;
account_info.account_id = CoreAccountId::FromString(client_id);
account_info.gaia = GaiaId(client_id);
@@ -97,11 +96,9 @@ signin::AccessTokenInfo BraveSyncAuthManager::GenerateAccessToken(
VLOG(1) << "timestamp=" << timestamp;
DCHECK(!timestamp.empty() && !public_key_.empty() && !private_key_.empty());
const std::string public_key_hex =
base::HexEncode(public_key_.data(), public_key_.size());
const std::string public_key_hex = base::HexEncode(public_key_);
const std::string timestamp_hex =
base::HexEncode(timestamp.data(), timestamp.size());
const std::string timestamp_hex = base::HexEncode(timestamp);
std::vector<uint8_t> timestamp_bytes;
base::HexStringToBytes(timestamp_hex, &timestamp_bytes);
@@ -109,8 +106,7 @@ signin::AccessTokenInfo BraveSyncAuthManager::GenerateAccessToken(
brave_sync::crypto::Sign(timestamp_bytes, private_key_, &signature);
DCHECK(brave_sync::crypto::Verify(timestamp_bytes, signature, public_key_));
const std::string signed_timestamp_hex =
base::HexEncode(signature.data(), signature.size());
const std::string signed_timestamp_hex = base::HexEncode(signature);
// base64(timestamp_hex|signed_timestamp_hex|public_key_hex)
const std::string access_token =
+2 -3
View File
@@ -144,7 +144,7 @@ void TorControl::OpenControl(int portno, std::vector<uint8_t> cookie) {
DCHECK(!running_);
running_ = true;
VLOG(3) << __func__ << " " << base::HexEncode(cookie.data(), cookie.size());
VLOG(3) << __func__ << " " << base::HexEncode(cookie);
net::AddressList addrlist = net::AddressList::CreateFromIPAddress(
net::IPAddress::IPv4Localhost(), portno);
@@ -185,8 +185,7 @@ void TorControl::Connected(std::vector<uint8_t> cookie, int rv) {
return;
}
DoCmd("AUTHENTICATE " + base::HexEncode(cookie.data(), cookie.size()),
base::DoNothing(),
DoCmd("AUTHENTICATE " + base::HexEncode(cookie), base::DoNothing(),
base::BindOnce(&TorControl::Authenticated,
weak_ptr_factory_.GetWeakPtr()));
}
+4 -2
View File
@@ -11,6 +11,7 @@
#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/containers/to_vector.h"
#include "base/files/file.h"
#include "base/logging.h"
#include "base/sequence_checker.h"
@@ -210,9 +211,10 @@ bool TorFileWatcher::EatControlCookie(std::vector<uint8_t>& cookie,
}
// Success!
cookie.assign(buf, UNSAFE_TODO(buf + *nread));
auto read_buffer = base::as_byte_span(buf).first(*nread);
cookie = base::ToVector(read_buffer);
mtime = info.last_accessed;
VLOG(3) << "Control cookie " << base::HexEncode(buf, *nread) << ", mtime "
VLOG(3) << "Control cookie " << base::HexEncode(read_buffer) << ", mtime "
<< mtime;
return true;
}
@@ -276,7 +276,7 @@ TorProxyMap::~TorProxyMap() {
std::string TorProxyMap::GenerateNewPassword() {
std::vector<uint8_t> password(kTorPasswordLength);
crypto::RandBytes(password);
return base::HexEncode(password.data(), password.size());
return base::HexEncode(password);
}
std::string TorProxyMap::Get(const std::string& username) {