Passwordfile library 5.0.13
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,
23};
24
26
35
37
39public:
40 explicit PasswordFile();
41 explicit PasswordFile(const std::string &path, const std::string &password);
42 PasswordFile(const PasswordFile &other);
45 CppUtilities::NativeFileStream &fileStream();
47 void opened();
48 void generateRootEntry();
49 void create();
50 void close();
51 void load();
52 std::uint32_t mininumVersion(PasswordFileSaveFlags options) const;
55 void clearEntries();
56 void clear();
57 void exportToTextfile(const std::string &targetPath) const;
58 void doBackup();
59 bool hasRootEntry() const;
60 const NodeEntry *rootEntry() const;
62 const std::string &path() const;
63 const std::string &password() const;
64 void setPath(const std::string &value);
65 void clearPath();
66 void setPassword(const std::string &password);
67 void setPassword(const char *password, const std::size_t passwordSize);
68 void clearPassword();
69 bool isEncryptionUsed();
70 bool isOpen() const;
71 std::string &extendedHeader();
72 const std::string &extendedHeader() const;
73 std::string &encryptedExtendedHeader();
74 const std::string &encryptedExtendedHeader() const;
75 std::size_t size();
76 std::uint32_t version() const;
79 std::string summary(PasswordFileSaveFlags saveOptions) const;
80
81private:
82 std::string m_path;
83 std::string m_password;
84 std::unique_ptr<NodeEntry> m_rootEntry;
85 std::string m_extendedHeader;
86 std::string m_encryptedExtendedHeader;
87 CppUtilities::NativeFileStream m_file;
88 CppUtilities::BinaryReader m_freader;
89 CppUtilities::BinaryWriter m_fwriter;
90 std::uint32_t m_version;
91 PasswordFileOpenFlags m_openOptions;
92 PasswordFileSaveFlags m_saveOptions;
93};
94
98inline CppUtilities::NativeFileStream &PasswordFile::fileStream()
99{
100 return m_file;
101}
102
106inline const std::string &PasswordFile::path() const
107{
108 return m_path;
109}
110
115{
116 close();
117 m_path.clear();
118}
119
123inline const std::string &PasswordFile::password() const
124{
125 return m_password;
126}
127
131inline void PasswordFile::setPassword(const std::string &password)
132{
133 m_password = password;
134}
135
139inline void PasswordFile::setPassword(const char *password, const size_t passwordSize)
140{
141 m_password.assign(password, passwordSize);
142}
143
148{
149 m_password.clear();
150}
151
155inline bool PasswordFile::isOpen() const
156{
157 return m_file.is_open();
158}
159
163inline std::string &PasswordFile::extendedHeader()
164{
165 return m_extendedHeader;
166}
167
171inline const std::string &PasswordFile::extendedHeader() const
172{
173 return m_extendedHeader;
174}
175
180{
181 return m_encryptedExtendedHeader;
182}
183
187inline const std::string &PasswordFile::encryptedExtendedHeader() const
188{
189 return m_encryptedExtendedHeader;
190}
191
196inline std::uint32_t PasswordFile::version() const
197{
198 return m_version;
199}
200
205{
206 return m_openOptions;
207}
208
213{
214 return m_saveOptions;
215}
216
217} // namespace Io
218
221
222#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)