SSP21-CPP
SecureFile.h
1 
2 #ifndef SSP21_SECUREFILE_H
3 #define SSP21_SECUREFILE_H
4 
5 #include "ssp21/util/SequenceTypes.h"
6 #include "ssp21/crypto/gen/FormatError.h"
7 
8 #include "ssp21/util/SerializationUtils.h"
9 #include "ssp21/util/SecureDynamicBuffer.h"
10 
11 #include <memory>
12 #include <fstream>
13 
14 namespace ssp21
15 {
16 
18  {
19 
20  public:
21 
22  static std::unique_ptr<SecureDynamicBuffer> read(const std::string& path, uint32_t max_file_size = 4096);
23 
24  template <class T>
25  static void write(const std::string& path, const T& item)
26  {
27  auto buffer = serialize::to_secure_buffer(item);
28  write(path, buffer->as_wslice());
29  }
30 
31  private:
32 
33  static void write(const std::string& path, const wseq32_t& data)
34  {
35  std::ofstream stream(path, std::ios::binary);
36 
37  if (!stream.is_open()) throw std::runtime_error(strings::join("Unable to open file: ", path));
38 
39  const uint8_t* buffer = data;
40 
41  if (!stream.write(reinterpret_cast<const char*>(buffer), data.length()))
42  {
43  throw std::runtime_error(strings::join("Unable to write file: ", path));
44  }
45  }
46 
47  };
48 
49 
50 }
51 
52 #endif
SSP21-cpp main namespace.
Definition: BufferTypes.h:12