Passwordfile library  5.0.6
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 #include <c++utilities/misc/flagenumclass.h>
10 
11 #include <cstdint>
12 #include <fstream>
13 #include <iostream>
14 #include <memory>
15 #include <string>
16 
17 namespace Io {
18 
19 class NodeEntry;
20 
21 enum class PasswordFileOpenFlags : std::uint64_t {
22  None = 0,
23  ReadOnly = 1,
24  Default = None,
25 };
26 
28 
29 enum class PasswordFileSaveFlags : std::uint64_t {
30  None = 0,
31  Encryption = 1,
32  Compression = 2,
33  PasswordHashing = 4,
36 };
37 
39 
41 public:
42  explicit PasswordFile();
43  explicit PasswordFile(const std::string &path, const std::string &password);
44  PasswordFile(const PasswordFile &other);
45  PasswordFile(PasswordFile &&other);
46  ~PasswordFile();
47  CppUtilities::NativeFileStream &fileStream();
49  void opened();
50  void generateRootEntry();
51  void create();
52  void close();
53  void load();
54  std::uint32_t mininumVersion(PasswordFileSaveFlags options) const;
57  void clearEntries();
58  void clear();
59  void exportToTextfile(const std::string &targetPath) const;
60  void doBackup();
61  bool hasRootEntry() const;
62  const NodeEntry *rootEntry() const;
63  NodeEntry *rootEntry();
64  const std::string &path() const;
65  const std::string &password() const;
66  void setPath(const std::string &value);
67  void clearPath();
68  void setPassword(const std::string &password);
69  void setPassword(const char *password, const std::size_t passwordSize);
70  void clearPassword();
71  bool isEncryptionUsed();
72  bool isOpen() const;
73  std::string &extendedHeader();
74  const std::string &extendedHeader() const;
75  std::string &encryptedExtendedHeader();
76  const std::string &encryptedExtendedHeader() const;
77  std::size_t size();
78  std::uint32_t version() const;
79  PasswordFileOpenFlags openOptions() const;
80  PasswordFileSaveFlags saveOptions() const;
81  std::string summary(PasswordFileSaveFlags saveOptions) const;
82 
83 private:
84  std::string m_path;
85  std::string m_password;
86  std::unique_ptr<NodeEntry> m_rootEntry;
87  std::string m_extendedHeader;
88  std::string m_encryptedExtendedHeader;
89  CppUtilities::NativeFileStream m_file;
90  CppUtilities::BinaryReader m_freader;
91  CppUtilities::BinaryWriter m_fwriter;
92  std::uint32_t m_version;
93  PasswordFileOpenFlags m_openOptions;
94  PasswordFileSaveFlags m_saveOptions;
95 };
96 
100 inline CppUtilities::NativeFileStream &PasswordFile::fileStream()
101 {
102  return m_file;
103 }
104 
108 inline const std::string &PasswordFile::path() const
109 {
110  return m_path;
111 }
112 
117 {
118  close();
119  m_path.clear();
120 }
121 
125 inline const std::string &PasswordFile::password() const
126 {
127  return m_password;
128 }
129 
133 inline void PasswordFile::setPassword(const std::string &password)
134 {
135  m_password = password;
136 }
137 
141 inline void PasswordFile::setPassword(const char *password, const size_t passwordSize)
142 {
143  m_password.assign(password, passwordSize);
144 }
145 
150 {
151  m_password.clear();
152 }
153 
157 inline bool PasswordFile::isOpen() const
158 {
159  return m_file.is_open();
160 }
161 
165 inline std::string &PasswordFile::extendedHeader()
166 {
167  return m_extendedHeader;
168 }
169 
173 inline const std::string &PasswordFile::extendedHeader() const
174 {
175  return m_extendedHeader;
176 }
177 
182 {
183  return m_encryptedExtendedHeader;
184 }
185 
189 inline const std::string &PasswordFile::encryptedExtendedHeader() const
190 {
191  return m_encryptedExtendedHeader;
192 }
193 
198 inline std::uint32_t PasswordFile::version() const
199 {
200  return m_version;
201 }
202 
207 {
208  return m_openOptions;
209 }
210 
215 {
216  return m_saveOptions;
217 }
218 
219 } // namespace Io
220 
223 
224 #endif // PASSWORD_FILE_IO_PASSWORD_FILE_H
The NodeEntry class acts as parent for other entries.
Definition: entry.h:114
The PasswordFile class holds account information in the form of Entry and Field instances and provide...
Definition: passwordfile.h:40
PasswordFileOpenFlags openOptions() const
Returns the options used to open the file.
Definition: passwordfile.h:206
bool isOpen() const
Returns an indication whether the file is open.
Definition: passwordfile.h:157
void close()
Closes the file if currently opened.
std::uint32_t 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:198
CppUtilities::NativeFileStream & fileStream()
Returns the underlying file stream.
Definition: passwordfile.h:100
void clearPassword()
Clears the current password.
Definition: passwordfile.h:149
const std::string & password() const
Returns the current password.
Definition: passwordfile.h:125
void setPassword(const std::string &password)
Sets the current password.
Definition: passwordfile.h:133
const std::string & path() const
Returns the current file path.
Definition: passwordfile.h:108
void setPassword(const char *password, const std::size_t passwordSize)
std::string & encryptedExtendedHeader()
Returns the encrypted extended header.
Definition: passwordfile.h:181
std::string & extendedHeader()
Returns the extended header.
Definition: passwordfile.h:165
void clearPath()
Clears the current path.
Definition: passwordfile.h:116
PasswordFileSaveFlags saveOptions() const
Returns the save options used the last time when saving the file.
Definition: passwordfile.h:214
#define PASSWORD_FILE_EXPORT
Marks the symbol to be exported by the passwordfile library.
Contains all IO related classes.
PASSWORD_FILE_EXPORT std::string flagsToString(PasswordFileOpenFlags flags)
Returns a comma-separated string for the specified flags.
PasswordFileSaveFlags
Definition: passwordfile.h:29
PasswordFileOpenFlags
Definition: passwordfile.h:21
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Io, Io::PasswordFileOpenFlags)