OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 // Bitwise operations on ByteVectors. These methods modify the lhs to be the |
| 21 // result of the bitwise AND, OR, or AND NOT. |
| 22 ByteVector* ByteVectorAnd(ByteVector* lhs, const ByteVector& rhs); |
| 23 ByteVector* ByteVectorOr(ByteVector* lhs, const ByteVector& rhs); |
| 24 ByteVector* ByteVectorMerge(ByteVector* lhs, |
| 25 const ByteVector& rhs, |
| 26 const ByteVector& mask); |
| 27 |
| 28 // A utility object for generating random binary data with different |
| 29 // likelihood of bits being true. |
| 30 class ByteVectorGenerator { |
| 31 public: |
| 32 explicit ByteVectorGenerator(size_t byte_count); |
| 33 // Generates a random byte vector where each bit is independently set with |
| 34 // the given probability. |
| 35 ByteVector GetWeightedRandomByteVector(Probability probability); |
| 36 |
| 37 protected: |
| 38 // Generates a random byte from a uniform distribution. |
| 39 virtual uint8_t RandByte(); |
| 40 |
| 41 private: |
| 42 // Generates a random vector of bytes from a uniform distribution. |
| 43 ByteVector GetRandomByteVector(); |
| 44 size_t byte_count_; |
| 45 }; |
| 46 |
| 47 // A ByteVectorGenerator that uses a psuedo-random function to generate |
| 48 // deterministicly random bits. |
| 49 class HmacByteVectorGenerator : public ByteVectorGenerator { |
| 50 public: |
| 51 HmacByteVectorGenerator(size_t byte_count, const std::string& secret); |
| 52 |
| 53 protected: |
| 54 virtual uint8_t RandByte(); |
| 55 |
| 56 private: |
| 57 crypto::HMAC hmac_; |
| 58 uint64_t hmac_state_; |
| 59 }; |
| 60 |
| 61 } // namespace rappor |
| 62 |
| 63 #endif // COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ |
OLD | NEW |