Passwordfile library 5.2.0
C++ library to read/write passwords from/to encrypted files
Loading...
Searching...
No Matches
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 <memory>
13#include <string>
14
15namespace Io {
16
17class NodeEntry;
18
19enum class PasswordFileOpenFlags : std::uint64_t {
20 None = 0,
21 ReadOnly = (1 << 0),
22 New = (1 << 1),
24};
25
27
37
39
41public:
42 explicit PasswordFile();
43 explicit PasswordFile(const std::string &path, const std::string &password);
44 PasswordFile(const PasswordFile &other);
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;
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;
81 std::string summary(PasswordFileSaveFlags saveOptions) const;
82
83private:
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
100inline CppUtilities::NativeFileStream &PasswordFile::fileStream()
101{
102 return m_file;
103}
104
108inline const std::string &PasswordFile::path() const
109{
110 return m_path;
111}
112
117{
118 close();
119 m_path.clear();
120}
121
125inline const std::string &PasswordFile::password() const
126{
127 return m_password;
128}
129
133inline void PasswordFile::setPassword(const std::string &password)
134{
135 m_password = password;
136}
137
141inline 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
157inline bool PasswordFile::isOpen() const
158{
159 return m_file.is_open();
160}
161
168inline std::string &PasswordFile::extendedHeader()
169{
170 return m_extendedHeader;
171}
172
176inline const std::string &PasswordFile::extendedHeader() const
177{
178 return m_extendedHeader;
179}
180
185{
186 return m_encryptedExtendedHeader;
187}
188
192inline const std::string &PasswordFile::encryptedExtendedHeader() const
193{
194 return m_encryptedExtendedHeader;
195}
196
201inline std::uint32_t PasswordFile::version() const
202{
203 return m_version;
204}
205
210{
211 return m_openOptions;
212}
213
218{
219 return m_saveOptions;
220}
221
222} // namespace Io
223
226
227#endif // PASSWORD_FILE_IO_PASSWORD_FILE_H
The NodeEntry class acts as parent for other entries.
Definition entry.h:114
PasswordFileOpenFlags openOptions() const
Returns the options used to open the file.
const NodeEntry * rootEntry() const
Returns the root entry if present or nullptr otherwise.
bool isOpen() const
Returns an indication whether the file is open.
void clear()
Closes the file if opened.
void opened()
Handles the file being opened.
void clearEntries()
Removes the root element if one is present.
void open(PasswordFileOpenFlags options=PasswordFileOpenFlags::Default)
Opens the file.
bool hasRootEntry() const
Returns an indication whether a root entry is present.
void create()
Creates the file.
void exportToTextfile(const std::string &targetPath) const
Writes the current root entry to a plain text file.
void load()
Reads the contents of the file.
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...
CppUtilities::NativeFileStream & fileStream()
Returns the underlying file stream.
void clearPassword()
Clears the current password.
std::string summary(PasswordFileSaveFlags saveOptions) const
Returns a summary about the file (version, used features, statistics).
PasswordFile()
Constructs a new password file.
const std::string & password() const
Returns the current password.
void setPassword(const std::string &password)
Sets the current password.
const std::string & path() const
Returns the current file path.
bool isEncryptionUsed()
Returns an indication whether encryption is used and the file is open; returns always false otherwise...
std::size_t size()
Returns the size of the file if the file is open; otherwise returns zero.
void setPassword(const char *password, const std::size_t passwordSize)
void save(PasswordFileSaveFlags options=PasswordFileSaveFlags::Default)
Writes the current root entry to the file under path() replacing its previous contents.
std::string & encryptedExtendedHeader()
Returns the encrypted extended header.
void setPath(const std::string &value)
Sets the current file path.
std::uint32_t mininumVersion(PasswordFileSaveFlags options) const
Returns the minimum file version required to write the current instance with the specified options.
std::string & extendedHeader()
Returns the extended header.
void write(PasswordFileSaveFlags options=PasswordFileSaveFlags::Default)
Writes the current root entry to the file which is assumed to be opened and writeable.
void generateRootEntry()
Generates a new root entry for the file.
void clearPath()
Clears the current path.
void doBackup()
Creates a backup of the file.
PasswordFileSaveFlags saveOptions() const
Returns the save options used the last time when saving the file.
#define PASSWORD_FILE_EXPORT
Marks the symbol to be exported by the passwordfile library.
Definition global.h:14
Contains all IO related classes.
PASSWORD_FILE_EXPORT std::string flagsToString(PasswordFileOpenFlags flags)
Returns a comma-separated string for the specified flags.
PasswordFileSaveFlags
PasswordFileOpenFlags
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Io, Io::PasswordFileOpenFlags)