| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/variations/entropy_provider.h" | 5 #include "components/variations/entropy_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/sha1.h" | 13 #include "base/sha1.h" |
| 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/sys_byteorder.h" | 15 #include "base/sys_byteorder.h" |
| 15 #include "components/variations/metrics_util.h" | 16 #include "components/variations/metrics_util.h" |
| 16 | 17 |
| 17 namespace metrics { | 18 namespace metrics { |
| 18 | 19 |
| 19 namespace internal { | 20 namespace internal { |
| 20 | 21 |
| 21 SeededRandGenerator::SeededRandGenerator(uint32_t seed) { | 22 SeededRandGenerator::SeededRandGenerator(uint32_t seed) { |
| 22 mersenne_twister_.init_genrand(seed); | 23 mersenne_twister_.init_genrand(seed); |
| 23 } | 24 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 const std::string& trial_name, | 81 const std::string& trial_name, |
| 81 uint32_t randomization_seed) const { | 82 uint32_t randomization_seed) const { |
| 82 // Given enough input entropy, SHA-1 will produce a uniformly random spread | 83 // Given enough input entropy, SHA-1 will produce a uniformly random spread |
| 83 // in its output space. In this case, the input entropy that is used is the | 84 // in its output space. In this case, the input entropy that is used is the |
| 84 // combination of the original |entropy_source_| and the |trial_name|. | 85 // combination of the original |entropy_source_| and the |trial_name|. |
| 85 // | 86 // |
| 86 // Note: If |entropy_source_| has very low entropy, such as 13 bits or less, | 87 // Note: If |entropy_source_| has very low entropy, such as 13 bits or less, |
| 87 // it has been observed that this method does not result in a uniform | 88 // it has been observed that this method does not result in a uniform |
| 88 // distribution given the same |trial_name|. When using such a low entropy | 89 // distribution given the same |trial_name|. When using such a low entropy |
| 89 // source, PermutedEntropyProvider should be used instead. | 90 // source, PermutedEntropyProvider should be used instead. |
| 90 std::string input(entropy_source_ + trial_name); | 91 std::string input(entropy_source_); |
| 92 input.append(randomization_seed == 0 ? trial_name : base::UintToString( |
| 93 randomization_seed)); |
| 94 |
| 91 unsigned char sha1_hash[base::kSHA1Length]; | 95 unsigned char sha1_hash[base::kSHA1Length]; |
| 92 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), | 96 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
| 93 input.size(), | 97 input.size(), |
| 94 sha1_hash); | 98 sha1_hash); |
| 95 | 99 |
| 96 uint64_t bits; | 100 uint64_t bits; |
| 97 static_assert(sizeof(bits) < sizeof(sha1_hash), "more data required"); | 101 static_assert(sizeof(bits) < sizeof(sha1_hash), "more data required"); |
| 98 memcpy(&bits, sha1_hash, sizeof(bits)); | 102 memcpy(&bits, sha1_hash, sizeof(bits)); |
| 99 bits = base::ByteSwapToLE64(bits); | 103 bits = base::ByteSwapToLE64(bits); |
| 100 | 104 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 123 } | 127 } |
| 124 | 128 |
| 125 uint16_t PermutedEntropyProvider::GetPermutedValue( | 129 uint16_t PermutedEntropyProvider::GetPermutedValue( |
| 126 uint32_t randomization_seed) const { | 130 uint32_t randomization_seed) const { |
| 127 std::vector<uint16_t> mapping(low_entropy_source_max_); | 131 std::vector<uint16_t> mapping(low_entropy_source_max_); |
| 128 internal::PermuteMappingUsingRandomizationSeed(randomization_seed, &mapping); | 132 internal::PermuteMappingUsingRandomizationSeed(randomization_seed, &mapping); |
| 129 return mapping[low_entropy_source_]; | 133 return mapping[low_entropy_source_]; |
| 130 } | 134 } |
| 131 | 135 |
| 132 } // namespace metrics | 136 } // namespace metrics |
| OLD | NEW |