Chromium Code Reviews| Index: components/rappor/byte_vector_utils.h |
| diff --git a/components/rappor/byte_vector_utils.h b/components/rappor/byte_vector_utils.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6b6284a09642b4842e80628992b4ba299855900c |
| --- /dev/null |
| +++ b/components/rappor/byte_vector_utils.h |
| @@ -0,0 +1,79 @@ |
| +// 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. |
| + |
| +#ifndef COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ |
| +#define COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ |
| + |
| +#include <stddef.h> |
| +#include <stdint.h> |
| +#include <vector> |
| + |
| +#include "components/rappor/rappor_parameters.h" |
| +#include "crypto/hmac.h" |
| + |
| +namespace rappor { |
| + |
| +// A vector of one byte integers used to store a set of binary bits. |
| +typedef std::vector<uint8_t> ByteVector; |
| + |
| +// Computes a bitwise OR of byte vectors and stores the result in rhs. |
| +ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs); |
| + |
| +// Merges the contents of lhs and rhs vectors according to a mask vector. |
| +// Equivalent to (lhs & ~mask) | (rhs & mask). Stores the result in rhs. |
| +ByteVector* ByteVectorMerge(const ByteVector& mask, |
| + const ByteVector& lhs, |
| + ByteVector* rhs); |
| + |
| +// Counts the number of bits set in the byte vector. |
| +int CountBits(const ByteVector& v); |
|
Alexei Svitkine (slow)
2014/01/24 22:09:03
Nit: Use more meaningful variable names than |v|.
|
| + |
| +// A utility object for generating random binary data with different |
| +// likelihood of bits being true. |
| +class ByteVectorGenerator { |
| + public: |
| + explicit ByteVectorGenerator(size_t byte_count); |
| + |
| + ~ByteVectorGenerator(); |
| + |
| + // Generates a random byte vector where each bit is independently set with |
| + // the given probability. |
| + ByteVector GetWeightedRandomByteVector(Probability probability); |
| + |
| + protected: |
| + // Generates a random byte from a uniform distribution. |
| + virtual uint8_t RandByte(); |
| + |
| + private: |
| + // Generates a random vector of bytes from a uniform distribution. |
| + ByteVector GetRandomByteVector(); |
| + |
| + size_t byte_count_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ByteVectorGenerator); |
| +}; |
| + |
| +// A ByteVectorGenerator that uses a psuedo-random function to generate |
| +// deterministicly random bits. |
| +class HmacByteVectorGenerator : public ByteVectorGenerator { |
| + public: |
| + // Constructor takes the size of the vector to generate, along with a |
| + // secret value to seed the pseudo-random number generator. |
| + HmacByteVectorGenerator(size_t byte_count, const std::string& secret); |
| + |
| + ~HmacByteVectorGenerator(); |
| + |
| + protected: |
| + virtual uint8_t RandByte() OVERRIDE; |
| + |
| + private: |
| + crypto::HMAC hmac_; |
| + uint64_t hmac_state_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(HmacByteVectorGenerator); |
| +}; |
| + |
| +} // namespace rappor |
| + |
| +#endif // COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ |