f1b86ea0 | /* Copyright (C) CJ Affiliate * * You may use, distribute and modify this code under the * terms of the GNU General Public License version 2 or * later. * * You should have received a copy of the license with this * file. If not, you will find a copy in the "LICENSE" file * at https://github.com/cjdev/dual-control. */ #include "random_source.h" #include "sys_fstream.h" namespace { |
1a0d77af | class invalid_random_source_exception : public std::exception {}; |
f1b86ea0 | class impl : public random_source_ifc { private: fstreams fstreams_; public: |
4a6aca25 | impl (const fstreams fstreams) |
f1b86ea0 | : fstreams_ (fstreams) {} |
88c14201 | std::vector<uint8_t> get_random_bytes (int length) const override { |
f1b86ea0 | fstreams::pstream random_source = fstreams_.open_fstream (file_path); std::vector<uint8_t> result (length); random_source->read (reinterpret_cast<char *> (result.data()), length); |
1a0d77af | if (random_source->fail()) { throw invalid_random_source_exception(); } |
f1b86ea0 | return result; } }; } |
1a0d77af | const std::string random_source_ifc::file_path = "/dev/urandom"; |
4a6aca25 | random_source random_source::create (const fstreams &fstreams) |
88c14201 | { return random_source (random_source::delegate (new impl (fstreams))); |
f1b86ea0 | }; |
a120158c |