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