Passwordfile library  3.1.4
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 
29 OpenSslRandomDevice::OpenSslRandomDevice()
30 {
31 }
32 
36 uint32 OpenSslRandomDevice::operator()() const
37 {
38  unsigned char buf[4];
39  if (RAND_bytes(buf, sizeof(buf))) {
40  return ConversionUtilities::LE::toUInt32(reinterpret_cast<char *>(buf));
41  } else {
42  string msg;
43  unsigned long errorCode = ERR_get_error();
44  while (errorCode != 0) {
45  if (!msg.empty()) {
46  msg += '\n';
47  }
48  msg += ERR_error_string(errorCode, 0);
49  errorCode = ERR_get_error();
50  }
51  throw Io::CryptoException(msg);
52  }
53 }
54 
58 bool OpenSslRandomDevice::status() const
59 {
60  return RAND_status();
61 }
62 }
STL namespace.
Contains utility classes and functions.
Definition: openssl.h:6
The exception that is thrown when an encryption/decryption error occurs.