diff options
Diffstat (limited to 'src/tests/test_rng.h')
-rw-r--r-- | src/tests/test_rng.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/tests/test_rng.h b/src/tests/test_rng.h new file mode 100644 index 000000000..b3da5f08a --- /dev/null +++ b/src/tests/test_rng.h @@ -0,0 +1,61 @@ + +#ifndef BOTAN_TESTS_FIXED_RNG_H__ +#define BOTAN_TESTS_FIXED_RNG_H__ + +#include <deque> +#include <string> +#include <stdexcept> +#include <botan/rng.h> +#include <botan/hex.h> + +using Botan::byte; + +class Fixed_Output_RNG : public Botan::RandomNumberGenerator + { + public: + bool is_seeded() const { return !buf.empty(); } + + byte random() + { + if(!is_seeded()) + throw std::runtime_error("Out of bytes"); + + byte out = buf.front(); + buf.pop_front(); + return out; + } + + void reseed(size_t) {} + + void randomize(byte out[], size_t len) + { + for(size_t j = 0; j != len; j++) + out[j] = random(); + } + + void add_entropy(const byte b[], size_t s) + { + buf.insert(buf.end(), b, b + s); + } + + std::string name() const { return "Fixed_Output_RNG"; } + + void clear() throw() {} + + Fixed_Output_RNG(const std::vector<byte>& in) + { + buf.insert(buf.end(), in.begin(), in.end()); + } + + Fixed_Output_RNG(const std::string& in_str) + { + std::vector<byte> in = Botan::hex_decode(in_str); + buf.insert(buf.end(), in.begin(), in.end()); + } + + Fixed_Output_RNG() {} + private: + std::deque<byte> buf; + }; + +#endif |