OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/rappor/rappor_metric.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 namespace rappor { | |
10 | |
11 RapporMetric::RapporMetric(const std::string& metric_name, | |
12 const RapporParameters& parameters, | |
13 int32_t cohort) | |
14 : metric_name_(metric_name), | |
15 parameters_(parameters), | |
16 bloom_(parameters.bloom_filter_size_bytes, | |
17 parameters.bloom_filter_hash_function_count, | |
18 cohort * parameters.bloom_filter_hash_function_count) { | |
19 DCHECK_GE(cohort, 0); | |
20 } | |
21 | |
22 RapporMetric::~RapporMetric() {} | |
23 | |
24 void RapporMetric::AddSample(const std::string& str) { bloom_.AddString(str); } | |
25 | |
26 ByteVector RapporMetric::GetReport(const std::string& secret) const { | |
27 // Generate a deterministically random mask of fake data using the | |
28 // client's secret key + real data as a seed. The inclusion of the secret | |
29 // in the seed prevents fake data from being correlated with real data. | |
ulfar
2014/02/07 20:19:02
// in the seed avoids correlations between the rea
Steven Holte
2014/02/07 21:08:16
Done.
| |
30 // The seed isn't a human-readable string. | |
31 std::string seed = secret + metric_name_ + | |
32 std::string(bytes().begin(), bytes().end()); | |
33 HmacByteVectorGenerator hmac_generator(bytes().size(), seed); | |
34 const ByteVector fake_mask = | |
35 hmac_generator.GetWeightedRandomByteVector(parameters().fake_prob); | |
36 ByteVector fake_ones = | |
37 hmac_generator.GetWeightedRandomByteVector(parameters().fake_one_prob); | |
38 | |
39 // Redact most of the real data by replacing it with the fake data, hiding | |
40 // and limiting the amount of information an individual client reports on. | |
41 const ByteVector* redacted_bits = | |
ulfar
2014/02/07 20:19:02
better name would be fake_and_redacted_bits
Steven Holte
2014/02/07 21:08:16
Done.
| |
42 ByteVectorMerge(fake_mask, bytes(), &fake_ones); | |
43 | |
44 // Generate biased coin flips for each bit. | |
45 ByteVectorGenerator coin_generator(bytes().size()); | |
46 const ByteVector zero_coins = | |
47 coin_generator.GetWeightedRandomByteVector(parameters().zero_coin_prob); | |
48 ByteVector one_coins = | |
49 coin_generator.GetWeightedRandomByteVector(parameters().one_coin_prob); | |
50 | |
51 // Use the redacted data to select which coin type is used for each bit in | |
52 // the final report. | |
53 return *ByteVectorMerge(*redacted_bits, zero_coins, &one_coins); | |
ulfar
2014/02/07 20:19:02
A better comment would be
// Create a randomized
Steven Holte
2014/02/07 21:08:16
Done.
| |
54 } | |
55 | |
56 } // namespace rappor | |
OLD | NEW |