Passwordfile library: util/opensslrandomdevice.cpp Source File

Passwordfile library 5.2.1 C++ library to read/write passwords from/to encrypted files

Loading...
Searching...
No Matches
opensslrandomdevice.cpp
Go to the documentation of this file.
1#include "./opensslrandomdevice.h"
2
3#include "../io/cryptoexception.h"
4
5#include <c++utilities/conversion/binaryconversion.h>
6
7#include <openssl/err.h>
8#include <openssl/rand.h>
9
10#include <string>
11
12using namespace CppUtilities;
13
14namespace Util {
15
20
24OpenSslRandomDevice::OpenSslRandomDevice()
25{
26}
27
31 OpenSslRandomDevice::result_type OpenSslRandomDevice::operator()() const
32{
33 unsigned char buf[4];
34 if (RAND_bytes(buf, sizeof(buf))) {
35 return LE::toUInt32(reinterpret_cast<char *>(buf));
36 }
37
38 // handle error case
39 std::string errorMsg;
40 while (unsigned long errorCode = ERR_get_error()) {
41 if (!errorMsg.empty()) {
42 errorMsg += '\n';
43 }
44 errorMsg += ERR_error_string(errorCode, nullptr);
45 }
46 throw Io::CryptoException(std::move(errorMsg));
47}
48
52 bool OpenSslRandomDevice::status() const
53{
54 return RAND_status();
55}
56} // namespace Util
Io::CryptoException
The exception that is thrown when an encryption/decryption error occurs.
Definition cryptoexception.h:11
Util::OpenSslRandomDevice::result_type
std::uint32_t result_type
Definition opensslrandomdevice.h:13
Util::OpenSslRandomDevice::operator()
result_type operator()() const
Generates a new random number.
Definition opensslrandomdevice.cpp:31
Util::OpenSslRandomDevice::status
bool status() const
Returns the status.
Definition opensslrandomdevice.cpp:52
Util::OpenSslRandomDevice::OpenSslRandomDevice
OpenSslRandomDevice()
Constructs a new random device.
Definition opensslrandomdevice.cpp:24
cryptoexception.h
CppUtilities
Definition openssl.h:13
Util
Contains utility classes and functions.
Definition openssl.h:17
opensslrandomdevice.h