| Index: components/rappor/byte_vector_utils.cc
|
| diff --git a/components/rappor/byte_vector_utils.cc b/components/rappor/byte_vector_utils.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..16beaa677263a77680efed662de22144de499329
|
| --- /dev/null
|
| +++ b/components/rappor/byte_vector_utils.cc
|
| @@ -0,0 +1,104 @@
|
| +// 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/byte_vector_utils.h"
|
| +
|
| +#include <string>
|
| +
|
| +#include "base/logging.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| +#include "crypto/random.h"
|
| +
|
| +namespace rappor {
|
| +
|
| +ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs) {
|
| + DCHECK_EQ(lhs.size(), rhs->size());
|
| + for (size_t i = 0, len = lhs.size(); i < len; ++i) {
|
| + (*rhs)[i] = lhs[i] | (*rhs)[i];
|
| + }
|
| + return rhs;
|
| +}
|
| +
|
| +ByteVector* ByteVectorMerge(const ByteVector& mask,
|
| + const ByteVector& lhs,
|
| + ByteVector* rhs) {
|
| + DCHECK_EQ(lhs.size(), rhs->size());
|
| + for (size_t i = 0, len = lhs.size(); i < len; ++i) {
|
| + (*rhs)[i] = (lhs[i] & ~mask[i]) | ((*rhs)[i] & mask[i]);
|
| + }
|
| + return rhs;
|
| +}
|
| +
|
| +int CountBits(const ByteVector& v) {
|
| + int bit_count = 0;
|
| + for(size_t i = 0; i < v.size(); ++i) {
|
| + uint8_t byte = v[i];
|
| + for (int j = 0; j < 8 ; ++j) {
|
| + if (byte & (1 << j))
|
| + bit_count++;
|
| + }
|
| + }
|
| + return bit_count;
|
| +}
|
| +
|
| +ByteVectorGenerator::ByteVectorGenerator(size_t byte_count)
|
| + : byte_count_(byte_count) {}
|
| +
|
| +ByteVectorGenerator::~ByteVectorGenerator() {}
|
| +
|
| +uint8_t ByteVectorGenerator::RandByte() {
|
| + uint8_t random_bits;
|
| + crypto::RandBytes(&random_bits, sizeof(uint8_t));
|
| + return random_bits;
|
| +}
|
| +
|
| +ByteVector ByteVectorGenerator::GetRandomByteVector() {
|
| + ByteVector bytes(byte_count_);
|
| + for (size_t i = 0; i < byte_count_; ++i) {
|
| + bytes[i] = RandByte();
|
| + }
|
| + return bytes;
|
| +}
|
| +
|
| +ByteVector ByteVectorGenerator::GetWeightedRandomByteVector(
|
| + Probability probability) {
|
| + ByteVector bytes = GetRandomByteVector();
|
| + switch (probability) {
|
| + case PROBABILITY_75:
|
| + return *ByteVectorOr(GetRandomByteVector(), &bytes);
|
| + case PROBABILITY_50:
|
| + return bytes;
|
| + }
|
| + NOTREACHED();
|
| + return bytes;
|
| +}
|
| +
|
| +HmacByteVectorGenerator::HmacByteVectorGenerator(size_t byte_count,
|
| + const std::string& secret)
|
| + : ByteVectorGenerator(byte_count),
|
| + hmac_(crypto::HMAC::SHA256) {
|
| + // "Nothing up my sleeve" number for the initial value of an
|
| + // HmacByteVectorGenerator's state. The initial digits of pi.
|
| + const uint64_t kHmacInitialState = 3141592653589793;
|
| +
|
| + if (!hmac_.Init(secret))
|
| + NOTREACHED();
|
| +
|
| + DCHECK_GT(hmac_.DigestLength(), sizeof(uint8_t));
|
| + hmac_state_ = kHmacInitialState;
|
| +}
|
| +
|
| +HmacByteVectorGenerator::~HmacByteVectorGenerator() {}
|
| +
|
| +uint8_t HmacByteVectorGenerator::RandByte() {
|
| + uint8_t random_bits;
|
| + std::string state = base::Uint64ToString(hmac_state_);
|
| + if (!hmac_.Sign(state, &random_bits, sizeof(uint8_t)))
|
| + NOTREACHED();
|
| +
|
| + ++hmac_state_;
|
| + return random_bits;
|
| +}
|
| +
|
| +} // namespace rappor
|
|
|