Passwordfile library  4.0.0
C++ library to read/write passwords from/to encrypted files
passwordfile.h
Go to the documentation of this file.
1 #ifndef PASSWORD_FILE_IO_PASSWORD_FILE_H
2 #define PASSWORD_FILE_IO_PASSWORD_FILE_H
3 
4 #include "../global.h"
5 
6 #include <c++utilities/io/binaryreader.h>
7 #include <c++utilities/io/binarywriter.h>
8 #include <c++utilities/io/nativefilestream.h>
9 
10 #include <fstream>
11 #include <iostream>
12 #include <memory>
13 #include <string>
14 
15 namespace Io {
16 
17 class NodeEntry;
18 
19 enum class PasswordFileOpenFlags : uint64 {
20  None = 0,
21  ReadOnly = 1,
22  Default = None,
23 };
24 
26 
28 {
29  return static_cast<PasswordFileOpenFlags>(
30  static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(lhs) | static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(rhs));
31 }
32 
34 {
35  return lhs = static_cast<PasswordFileOpenFlags>(static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(lhs)
36  | static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(rhs));
37 }
38 
40 {
41  return static_cast<bool>(
42  static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(lhs) & static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(rhs));
43 }
44 
45 enum class PasswordFileSaveFlags : uint64 {
46  None = 0,
47  Encryption = 1,
48  Compression = 2,
49  PasswordHashing = 4,
51 };
52 
54 
56 {
57  return static_cast<PasswordFileSaveFlags>(
58  static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(lhs) | static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(rhs));
59 }
60 
62 {
63  return lhs = static_cast<PasswordFileSaveFlags>(static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(lhs)
64  | static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(rhs));
65 }
66 
68 {
69  return static_cast<bool>(
70  static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(lhs) & static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(rhs));
71 }
72 
74 public:
75  explicit PasswordFile();
76  explicit PasswordFile(const std::string &path, const std::string &password);
77  PasswordFile(const PasswordFile &other);
78  PasswordFile(PasswordFile &&other);
79  ~PasswordFile();
80  IoUtilities::NativeFileStream &fileStream();
82  void opened();
83  void generateRootEntry();
84  void create();
85  void close();
86  void load();
87  uint32 mininumVersion(PasswordFileSaveFlags options) const;
90  void clearEntries();
91  void clear();
92  void exportToTextfile(const std::string &targetPath) const;
93  void doBackup();
94  bool hasRootEntry() const;
95  const NodeEntry *rootEntry() const;
96  NodeEntry *rootEntry();
97  const std::string &path() const;
98  const std::string &password() const;
99  void setPath(const std::string &value);
100  void clearPath();
101  void setPassword(const std::string &password);
102  void setPassword(const char *password, const std::size_t passwordSize);
103  void clearPassword();
104  bool isEncryptionUsed();
105  bool isOpen() const;
106  std::string &extendedHeader();
107  const std::string &extendedHeader() const;
108  std::string &encryptedExtendedHeader();
109  const std::string &encryptedExtendedHeader() const;
110  std::size_t size();
111  uint32 version() const;
112  PasswordFileOpenFlags openOptions() const;
113  PasswordFileSaveFlags saveOptions() const;
114  std::string summary(PasswordFileSaveFlags saveOptions) const;
115 
116 private:
117  std::string m_path;
118  std::string m_password;
119  std::unique_ptr<NodeEntry> m_rootEntry;
120  std::string m_extendedHeader;
121  std::string m_encryptedExtendedHeader;
122  IoUtilities::NativeFileStream m_file;
123  IoUtilities::BinaryReader m_freader;
124  IoUtilities::BinaryWriter m_fwriter;
125  uint32 m_version;
126  PasswordFileOpenFlags m_openOptions;
127  PasswordFileSaveFlags m_saveOptions;
128 };
129 
133 inline IoUtilities::NativeFileStream &PasswordFile::fileStream()
134 {
135  return m_file;
136 }
137 
141 inline const std::string &PasswordFile::path() const
142 {
143  return m_path;
144 }
145 
150 {
151  close();
152  m_path.clear();
153 }
154 
158 inline const std::string &PasswordFile::password() const
159 {
160  return m_password;
161 }
162 
166 inline void PasswordFile::setPassword(const std::string &password)
167 {
168  m_password = password;
169 }
170 
174 inline void PasswordFile::setPassword(const char *password, const size_t passwordSize)
175 {
176  m_password.assign(password, passwordSize);
177 }
178 
183 {
184  m_password.clear();
185 }
186 
190 inline bool PasswordFile::isOpen() const
191 {
192  return m_file.is_open();
193 }
194 
198 inline std::string &PasswordFile::extendedHeader()
199 {
200  return m_extendedHeader;
201 }
202 
206 inline const std::string &PasswordFile::extendedHeader() const
207 {
208  return m_extendedHeader;
209 }
210 
215 {
216  return m_encryptedExtendedHeader;
217 }
218 
222 inline const std::string &PasswordFile::encryptedExtendedHeader() const
223 {
224  return m_encryptedExtendedHeader;
225 }
226 
231 inline uint32 PasswordFile::version() const
232 {
233  return m_version;
234 }
235 
240 {
241  return m_openOptions;
242 }
243 
248 {
249  return m_saveOptions;
250 }
251 
252 } // namespace Io
253 
254 #endif // PASSWORD_FILE_IO_PASSWORD_FILE_H
The NodeEntry class acts as parent for other entries.
Definition: entry.h:115
The PasswordFile class holds account information in the form of Entry and Field instances and provide...
Definition: passwordfile.h:73
std::string & encryptedExtendedHeader()
Returns the encrypted extended header.
Definition: passwordfile.h:214
std::string PASSWORD_FILE_EXPORT flagsToString(PasswordFileOpenFlags flags)
Returns a comma-separated string for the specified flags.
void close()
Closes the file if currently opened.
Contains all IO related classes.
PasswordFileOpenFlags openOptions() const
Returns the options used to open the file.
Definition: passwordfile.h:239
PasswordFileOpenFlags
Definition: passwordfile.h:19
bool isOpen() const
Returns an indication whether the file is open.
Definition: passwordfile.h:190
void clearPath()
Clears the current path.
Definition: passwordfile.h:149
const std::string & path() const
Returns the current file path.
Definition: passwordfile.h:141
PasswordFileSaveFlags
Definition: passwordfile.h:45
void setPassword(const std::string &password)
Sets the current password.
Definition: passwordfile.h:166
#define PASSWORD_FILE_EXPORT
Marks the symbol to be exported by the passwordfile library.
std::string & extendedHeader()
Returns the extended header.
Definition: passwordfile.h:198
const std::string & password() const
Returns the current password.
Definition: passwordfile.h:158
constexpr bool operator &(PasswordFileOpenFlags lhs, PasswordFileOpenFlags rhs)
Definition: passwordfile.h:39
uint32 version() const
Returns the file version used the last time when saving the file (the version of the file as it is on...
Definition: passwordfile.h:231
void clearPassword()
Clears the current password.
Definition: passwordfile.h:182
constexpr PasswordFileOpenFlags operator|(PasswordFileOpenFlags lhs, PasswordFileOpenFlags rhs)
Definition: passwordfile.h:27
constexpr PasswordFileOpenFlags & operator|=(PasswordFileOpenFlags &lhs, PasswordFileOpenFlags rhs)
Definition: passwordfile.h:33
IoUtilities::NativeFileStream & fileStream()
Returns the underlying file stream.
Definition: passwordfile.h:133
PasswordFileSaveFlags saveOptions() const
Returns the save options used the last time when saving the file.
Definition: passwordfile.h:247