Passwordfile library  3.2.0
C++ library to read/write passwords from/to encrypted files
opensslrandomdevice.cpp
Go to the documentation of this file.
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 
12 using namespace std;
13 
14 namespace Util {
15 
24 OpenSslRandomDevice::OpenSslRandomDevice()
25 {
26 }
27 
31 uint32 OpenSslRandomDevice::operator()() const
32 {
33  unsigned char buf[4];
34  if (RAND_bytes(buf, sizeof(buf))) {
35  return ConversionUtilities::LE::toUInt32(reinterpret_cast<char *>(buf));
36  }
37 
38  // handle error case
39  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  errorCode = ERR_get_error();
46  }
47  throw Io::CryptoException(errorMsg);
48 }
49 
53 bool OpenSslRandomDevice::status() const
54 {
55  return RAND_status();
56 }
57 } // namespace Util
STL namespace.
Contains utility classes and functions.
Definition: openssl.h:6
The exception that is thrown when an encryption/decryption error occurs.