1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_BROWSING_TOPICS_UTIL_H_
6 #define COMPONENTS_BROWSING_TOPICS_UTIL_H_
8 #include "base/containers/span.h"
9 #include "base/time/time.h"
10 #include "components/browsing_topics/common/common_types.h"
12 namespace browsing_topics {
14 using HmacKey = std::array<uint8_t, 32>;
15 using ReadOnlyHmacKey = base::span<const uint8_t, 32>;
17 // Generate a 256 bit random hmac key.
18 HmacKey GenerateRandomHmacKey();
20 // Returns a per-user, per-epoch hash of `top_domain` for the purpose of
21 // deciding whether to return the random or top topic. The `hmac_key` is
22 // per-use and `epoch_calculation_time` represents the epoch.
23 uint64_t HashTopDomainForRandomOrTopTopicDecision(
24 ReadOnlyHmacKey hmac_key,
25 base::Time epoch_calculation_time,
26 const std::string& top_domain);
28 // Returns a per-user, per-epoch hash of `top_domain` for the purpose of
29 // deciding which random topic among the full taxonomy should be returned. The
30 // `hmac_key` is per-use and `epoch_calculation_time` represents the epoch.
31 uint64_t HashTopDomainForRandomTopicIndexDecision(
32 ReadOnlyHmacKey hmac_key,
33 base::Time epoch_calculation_time,
34 const std::string& top_domain);
36 // Returns a per-user, per-epoch hash of `top_domain` for the purpose of
37 // deciding which top topic to return. The `hmac_key` is per-user and
38 // `epoch_calculation_time` represents the epoch.
39 uint64_t HashTopDomainForTopTopicIndexDecision(
40 ReadOnlyHmacKey hmac_key,
41 base::Time epoch_calculation_time,
42 const std::string& top_domain);
44 // Returns a per-user hash of `top_domain` for the purpose of deciding the epoch
45 // switch-over time. The `hmac_key` is per-user.
46 uint64_t HashTopDomainForEpochSwitchTimeDecision(ReadOnlyHmacKey hmac_key,
47 const std::string& top_domain);
49 // Returns a per-user hash of `context_domain` to be stored more efficiently in
50 // disk and memory. The `hmac_key` is per-user. A per-user hash is necessary to
51 // prevent a context from learning the topics that don't belong to it via
53 HashedDomain HashContextDomainForStorage(ReadOnlyHmacKey hmac_key,
54 const std::string& context_domain);
56 // Returns a hash of `main_frame_host` to be stored more efficiently in disk and
58 HashedHost HashMainFrameHostForStorage(const std::string& main_frame_host);
60 // Override the key to be returned for subsequent invocations of
61 // `GenerateRandomHmacKey()`.
62 void OverrideHmacKeyForTesting(ReadOnlyHmacKey hmac_key);
64 } // namespace browsing_topics
66 #endif // COMPONENTS_BROWSING_TOPICS_UTIL_H_