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 #ifndef COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ |
| 6 #define COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 #include <vector> |
| 11 |
| 12 #include "components/rappor/rappor_parameters.h" |
| 13 #include "crypto/hmac.h" |
| 14 |
| 15 namespace rappor { |
| 16 |
| 17 // A vector of one byte integers used to store a set of binary bits. |
| 18 typedef std::vector<uint8_t> ByteVector; |
| 19 |
| 20 // Computes a bitwise OR of byte vectors and stores the result in rhs. |
| 21 ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs); |
| 22 |
| 23 // Merges the contents of lhs and rhs vectors according to a mask vector. |
| 24 // Equivalent to (lhs & ~mask) | (rhs & mask). Stores the result in rhs. |
| 25 ByteVector* ByteVectorMerge(const ByteVector& mask, |
| 26 const ByteVector& lhs, |
| 27 ByteVector* rhs); |
| 28 |
| 29 // A utility object for generating random binary data with different |
| 30 // likelihood of bits being true. |
| 31 class ByteVectorGenerator { |
| 32 public: |
| 33 explicit ByteVectorGenerator(size_t byte_count); |
| 34 |
| 35 ~ByteVectorGenerator(); |
| 36 |
| 37 // Generates a random byte vector where each bit is independently set with |
| 38 // the given probability. |
| 39 ByteVector GetWeightedRandomByteVector(Probability probability); |
| 40 |
| 41 protected: |
| 42 // Generates a random byte from a uniform distribution. |
| 43 virtual uint8_t RandByte(); |
| 44 |
| 45 private: |
| 46 // Generates a random vector of bytes from a uniform distribution. |
| 47 ByteVector GetRandomByteVector(); |
| 48 |
| 49 size_t byte_count_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(ByteVectorGenerator); |
| 52 }; |
| 53 |
| 54 // A ByteVectorGenerator that uses a psuedo-random function to generate |
| 55 // deterministicly random bits. |
| 56 class HmacByteVectorGenerator : public ByteVectorGenerator { |
| 57 public: |
| 58 // Constructor takes the size of the vector to generate, along with a |
| 59 // secret value to seed the pseudo-random number generator. |
| 60 HmacByteVectorGenerator(size_t byte_count, const std::string& secret); |
| 61 |
| 62 ~HmacByteVectorGenerator(); |
| 63 |
| 64 protected: |
| 65 virtual uint8_t RandByte() OVERRIDE; |
| 66 |
| 67 private: |
| 68 crypto::HMAC hmac_; |
| 69 uint64_t hmac_state_; |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(HmacByteVectorGenerator); |
| 72 }; |
| 73 |
| 74 } // namespace rappor |
| 75 |
| 76 #endif // COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ |
OLD | NEW |