Index: components/rappor/rappor_metric.cc |
diff --git a/components/rappor/rappor_metric.cc b/components/rappor/rappor_metric.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a6e6cfcb00cb6a34d9d57a38e929e59511db17ea |
--- /dev/null |
+++ b/components/rappor/rappor_metric.cc |
@@ -0,0 +1,53 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/rappor/rappor_metric.h" |
+ |
+namespace rappor { |
+ |
+RapporMetric::RapporMetric(const RapporParameters& parameters) |
+ : parameters_(parameters), |
+ bloom_(parameters.bloom_filter_size_bytes, |
+ parameters.bloom_filter_hash_function_count) {} |
+ |
+void RapporMetric::AddSamples(const std::vector<std::string>& strings) { |
+ bloom_.AddStrings(strings); |
+} |
+ |
+void RapporMetric::AddSample(const std::string& str) { bloom_.AddString(str); } |
+ |
+ByteVector RapporMetric::GetReport(const std::string& secret) const { |
+ // Start with the real bloom filter data. |
+ const ByteVector real_bits(bytes()); |
+ |
+ // Generate a deterministically random mask of fake data based on the |
+ // client's secret key. |
+ HmacByteVectorGenerator hmac_generator(real_bits.size(), |
+ secret + parameters()->rappor_name); |
+ const ByteVector fake_mask = |
+ hmac_generator.GetWeightedRandomByteVector(parameters()->fake_prob); |
+ ByteVector fake_ones = |
+ hmac_generator.GetWeightedRandomByteVector(parameters()->fake_one_prob); |
+ |
+ // Redact most of the real data by replacing it with the fake data, hiding |
+ // and limiting the amount of information an individual client reports on. |
+ const ByteVector* redacted_bits = |
+ ByteVectorMerge(fake_mask, real_bits, &fake_ones); |
+ |
+ // Generate biased coin flips for each bit. |
+ ByteVectorGenerator coin_generator(real_bits.size()); |
+ const ByteVector zero_coins = |
+ coin_generator.GetWeightedRandomByteVector(parameters()->zero_coin_prob); |
+ ByteVector one_coins = |
+ coin_generator.GetWeightedRandomByteVector(parameters()->one_coin_prob); |
+ |
+ // Use the redacted data to select which coin type is used for each bit in |
+ // the final report. |
+ const ByteVector* output = |
+ ByteVectorMerge(*redacted_bits, zero_coins, &one_coins); |
+ |
+ return *output; |
+} |
+ |
+} // namespace rappor |