tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

explicit_seed_seq.h (2887B)


      1 // Copyright 2017 The Abseil Authors.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //      https://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #ifndef ABSL_RANDOM_INTERNAL_EXPLICIT_SEED_SEQ_H_
     16 #define ABSL_RANDOM_INTERNAL_EXPLICIT_SEED_SEQ_H_
     17 
     18 #include <algorithm>
     19 #include <cstddef>
     20 #include <cstdint>
     21 #include <initializer_list>
     22 #include <iterator>
     23 #include <vector>
     24 
     25 #include "absl/base/config.h"
     26 #include "absl/base/internal/endian.h"
     27 
     28 namespace absl {
     29 ABSL_NAMESPACE_BEGIN
     30 namespace random_internal {
     31 
     32 // This class conforms to the C++ Standard "Seed Sequence" concept
     33 // [rand.req.seedseq].
     34 //
     35 // An "ExplicitSeedSeq" is meant to provide a conformant interface for
     36 // forwarding pre-computed seed material to the constructor of a class
     37 // conforming to the "Uniform Random Bit Generator" concept. This class makes no
     38 // attempt to mutate the state provided by its constructor, and returns it
     39 // directly via ExplicitSeedSeq::generate().
     40 //
     41 // If this class is asked to generate more seed material than was provided to
     42 // the constructor, then the remaining bytes will be filled with deterministic,
     43 // nonrandom data.
     44 class ExplicitSeedSeq {
     45 public:
     46  using result_type = uint32_t;
     47 
     48  ExplicitSeedSeq() : state_() {}
     49 
     50  // Copy and move both allowed.
     51  ExplicitSeedSeq(const ExplicitSeedSeq& other) = default;
     52  ExplicitSeedSeq& operator=(const ExplicitSeedSeq& other) = default;
     53  ExplicitSeedSeq(ExplicitSeedSeq&& other) = default;
     54  ExplicitSeedSeq& operator=(ExplicitSeedSeq&& other) = default;
     55 
     56  template <typename Iterator>
     57  ExplicitSeedSeq(Iterator begin, Iterator end) {
     58    for (auto it = begin; it != end; it++) {
     59      state_.push_back(*it & 0xffffffff);
     60    }
     61  }
     62 
     63  template <typename T>
     64  ExplicitSeedSeq(std::initializer_list<T> il)
     65      : ExplicitSeedSeq(il.begin(), il.end()) {}
     66 
     67  size_t size() const { return state_.size(); }
     68 
     69  template <typename OutIterator>
     70  void param(OutIterator out) const {
     71    std::copy(std::begin(state_), std::end(state_), out);
     72  }
     73 
     74  template <typename OutIterator>
     75  void generate(OutIterator begin, OutIterator end) {
     76    for (size_t index = 0; begin != end; begin++) {
     77      *begin = state_.empty() ? 0 : state_[index++];
     78      if (index >= state_.size()) {
     79        index = 0;
     80      }
     81    }
     82  }
     83 
     84 protected:
     85  std::vector<uint32_t> state_;
     86 };
     87 
     88 }  // namespace random_internal
     89 ABSL_NAMESPACE_END
     90 }  // namespace absl
     91 
     92 #endif  // ABSL_RANDOM_INTERNAL_EXPLICIT_SEED_SEQ_H_