Passwordfile library: io/passwordfile.h Source File

Passwordfile library 5.2.1 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
19 enum class PasswordFileOpenFlags : std::uint64_t {
20 None = 0,
21 ReadOnly = (1 << 0),
22 New = (1 << 1),
23 Default = None,
24};
25
26 PASSWORD_FILE_EXPORT std::string flagsToString(PasswordFileOpenFlags flags);
27
28 enum class PasswordFileSaveFlags : std::uint64_t {
29 None = 0,
30 Encryption = (1 << 0),
31 Compression = (1 << 1),
32 PasswordHashing = (1 << 2),
33 AllowToCreateNewFile = (1 << 3),
34 AuthenticationTag = (1 << 4),
35 Default = Encryption | Compression | PasswordHashing | AllowToCreateNewFile
36 | AuthenticationTag,
37};
38
39 PASSWORD_FILE_EXPORT std::string flagsToString(PasswordFileSaveFlags flags);
40
41 class PASSWORD_FILE_EXPORT PasswordFile {
42public:
43 explicit PasswordFile();
44 explicit PasswordFile(const std::string &path, const std::string &password);
45 PasswordFile(const PasswordFile &other);
46 PasswordFile(PasswordFile &&other);
47 ~PasswordFile();
48 CppUtilities::NativeFileStream &fileStream();
49 void open(PasswordFileOpenFlags options = PasswordFileOpenFlags::Default);
50 void opened();
51 void generateRootEntry();
52 void create();
53 void close();
54 void load();
55 std::uint32_t mininumVersion(PasswordFileSaveFlags options) const;
56 void save(PasswordFileSaveFlags options = PasswordFileSaveFlags::Default);
57 void write(PasswordFileSaveFlags options = PasswordFileSaveFlags::Default);
58 void clearEntries();
59 void clear();
60 void exportToTextfile(const std::string &targetPath) const;
61 void doBackup();
62 bool hasRootEntry() const;
63 const NodeEntry *rootEntry() const;
64 NodeEntry *rootEntry();
65 const std::string &path() const;
66 const std::string &password() const;
67 void setPath(const std::string &value);
68 void clearPath();
69 void setPassword(const std::string &password);
70 void setPassword(const char *password, const std::size_t passwordSize);
71 void clearPassword();
72 bool isEncryptionUsed();
73 bool isOpen() const;
74 std::string &extendedHeader();
75 const std::string &extendedHeader() const;
76 std::string &encryptedExtendedHeader();
77 const std::string &encryptedExtendedHeader() const;
78 std::size_t size();
79 std::uint32_t version() const;
80 PasswordFileOpenFlags openOptions() const;
81 PasswordFileSaveFlags saveOptions() const;
82 std::string summary(PasswordFileSaveFlags saveOptions) const;
83
84private:
85 std::string m_path;
86 std::string m_password;
87 std::unique_ptr<NodeEntry> m_rootEntry;
88 std::string m_extendedHeader;
89 std::string m_encryptedExtendedHeader;
90 CppUtilities::NativeFileStream m_file;
91 CppUtilities::BinaryReader m_freader;
92 CppUtilities::BinaryWriter m_fwriter;
93 std::uint32_t m_version;
94 PasswordFileOpenFlags m_openOptions;
95 PasswordFileSaveFlags m_saveOptions;
96};
97
101 inline CppUtilities::NativeFileStream &PasswordFile::fileStream()
102{
103 return m_file;
104}
105
109 inline const std::string &PasswordFile::path() const
110{
111 return m_path;
112}
113
117 inline void PasswordFile::clearPath()
118{
119 close();
120 m_path.clear();
121}
122
126 inline const std::string &PasswordFile::password() const
127{
128 return m_password;
129}
130
134 inline void PasswordFile::setPassword(const std::string &password)
135{
136 m_password = password;
137}
138
142inline void PasswordFile::setPassword(const char *password, const size_t passwordSize)
143{
144 m_password.assign(password, passwordSize);
145}
146
150 inline void PasswordFile::clearPassword()
151{
152 m_password.clear();
153}
154
158 inline bool PasswordFile::isOpen() const
159{
160 return m_file.is_open();
161}
162
169 inline std::string &PasswordFile::extendedHeader()
170{
171 return m_extendedHeader;
172}
173
177 inline const std::string &PasswordFile::extendedHeader() const
178{
179 return m_extendedHeader;
180}
181
185 inline std::string &PasswordFile::encryptedExtendedHeader()
186{
187 return m_encryptedExtendedHeader;
188}
189
193 inline const std::string &PasswordFile::encryptedExtendedHeader() const
194{
195 return m_encryptedExtendedHeader;
196}
197
202 inline std::uint32_t PasswordFile::version() const
203{
204 return m_version;
205}
206
210 inline PasswordFileOpenFlags PasswordFile::openOptions() const
211{
212 return m_openOptions;
213}
214
218 inline PasswordFileSaveFlags PasswordFile::saveOptions() const
219{
220 return m_saveOptions;
221}
222
223} // namespace Io
224
225CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Io, Io::PasswordFileOpenFlags);
226 CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Io, Io::PasswordFileSaveFlags);
227
228#endif // PASSWORD_FILE_IO_PASSWORD_FILE_H
Io::NodeEntry
The NodeEntry class acts as parent for other entries.
Definition entry.h:114
Io::PasswordFile::openOptions
PasswordFileOpenFlags openOptions() const
Returns the options used to open the file.
Definition passwordfile.h:210
Io::PasswordFile::rootEntry
const NodeEntry * rootEntry() const
Returns the root entry if present or nullptr otherwise.
Definition passwordfile.cpp:770
Io::PasswordFile::isOpen
bool isOpen() const
Returns an indication whether the file is open.
Definition passwordfile.h:158
Io::PasswordFile::clear
void clear()
Closes the file if opened.
Definition passwordfile.cpp:675
Io::PasswordFile::opened
void opened()
Handles the file being opened.
Definition passwordfile.cpp:149
Io::PasswordFile::clearEntries
void clearEntries()
Removes the root element if one is present.
Definition passwordfile.cpp:667
Io::PasswordFile::open
void open(PasswordFileOpenFlags options=PasswordFileOpenFlags::Default)
Opens the file.
Definition passwordfile.cpp:129
Io::PasswordFile::hasRootEntry
bool hasRootEntry() const
Returns an indication whether a root entry is present.
Definition passwordfile.cpp:762
Io::PasswordFile::create
void create()
Creates the file.
Definition passwordfile.cpp:173
Io::PasswordFile::exportToTextfile
void exportToTextfile(const std::string &targetPath) const
Writes the current root entry to a plain text file.
Definition passwordfile.cpp:691
Io::PasswordFile::load
void load()
Reads the contents of the file.
Definition passwordfile.cpp:191
Io::PasswordFile::close
void close()
Closes the file if currently opened.
Definition passwordfile.cpp:786
Io::PasswordFile::version
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:202
Io::PasswordFile::fileStream
CppUtilities::NativeFileStream & fileStream()
Returns the underlying file stream.
Definition passwordfile.h:101
Io::PasswordFile::clearPassword
void clearPassword()
Clears the current password.
Definition passwordfile.h:150
Io::PasswordFile::summary
std::string summary(PasswordFileSaveFlags saveOptions) const
Returns a summary about the file (version, used features, statistics).
Definition passwordfile.cpp:851
Io::PasswordFile::PasswordFile
PasswordFile()
Constructs a new password file.
Definition passwordfile.cpp:56
Io::PasswordFile::password
const std::string & password() const
Returns the current password.
Definition passwordfile.h:126
Io::PasswordFile::setPassword
void setPassword(const std::string &password)
Sets the current password.
Definition passwordfile.h:134
Io::PasswordFile::path
const std::string & path() const
Returns the current file path.
Definition passwordfile.h:109
Io::PasswordFile::isEncryptionUsed
bool isEncryptionUsed()
Returns an indication whether encryption is used and the file is open; returns always false otherwise...
Definition passwordfile.cpp:813
Io::PasswordFile::size
std::size_t size()
Returns the size of the file if the file is open; otherwise returns zero.
Definition passwordfile.cpp:839
Io::PasswordFile::setPassword
void setPassword(const char *password, const std::size_t passwordSize)
Io::PasswordFile::save
void save(PasswordFileSaveFlags options=PasswordFileSaveFlags::Default)
Writes the current root entry to the file under path() replacing its previous contents.
Definition passwordfile.cpp:457
Io::PasswordFile::encryptedExtendedHeader
std::string & encryptedExtendedHeader()
Returns the encrypted extended header.
Definition passwordfile.h:185
Io::PasswordFile::setPath
void setPath(const std::string &value)
Sets the current file path.
Definition passwordfile.cpp:797
Io::PasswordFile::mininumVersion
std::uint32_t mininumVersion(PasswordFileSaveFlags options) const
Returns the minimum file version required to write the current instance with the specified options.
Definition passwordfile.cpp:435
Io::PasswordFile::extendedHeader
std::string & extendedHeader()
Returns the extended header.
Definition passwordfile.h:169
Io::PasswordFile::write
void write(PasswordFileSaveFlags options=PasswordFileSaveFlags::Default)
Writes the current root entry to the file which is assumed to be opened and writeable.
Definition passwordfile.cpp:503
Io::PasswordFile::generateRootEntry
void generateRootEntry()
Generates a new root entry for the file.
Definition passwordfile.cpp:162
Io::PasswordFile::clearPath
void clearPath()
Clears the current path.
Definition passwordfile.h:117
Io::PasswordFile::doBackup
void doBackup()
Creates a backup of the file.
Definition passwordfile.cpp:733
Io::PasswordFile::saveOptions
PasswordFileSaveFlags saveOptions() const
Returns the save options used the last time when saving the file.
Definition passwordfile.h:218
global.h
PASSWORD_FILE_EXPORT
#define PASSWORD_FILE_EXPORT
Marks the symbol to be exported by the passwordfile library.
Definition global.h:14
Io
Contains all IO related classes.
Definition cryptoexception.h:9
Io::flagsToString
PASSWORD_FILE_EXPORT std::string flagsToString(PasswordFileOpenFlags flags)
Returns a comma-separated string for the specified flags.
Definition passwordfile.cpp:875
Io::PasswordFileSaveFlags
PasswordFileSaveFlags
Definition passwordfile.h:28
Io::PasswordFileSaveFlags::Default
@ Default
Definition passwordfile.h:35
Io::PasswordFileSaveFlags::AuthenticationTag
@ AuthenticationTag
Definition passwordfile.h:34
Io::PasswordFileSaveFlags::PasswordHashing
@ PasswordHashing
Definition passwordfile.h:32
Io::PasswordFileSaveFlags::Encryption
@ Encryption
Definition passwordfile.h:30
Io::PasswordFileOpenFlags
PasswordFileOpenFlags
Definition passwordfile.h:19
Io::PasswordFileOpenFlags::ReadOnly
@ ReadOnly
Definition passwordfile.h:21
Io::PasswordFileOpenFlags::None
@ None
Definition passwordfile.h:20
Io::PasswordFileOpenFlags::Default
@ Default
Definition passwordfile.h:23
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Io, Io::PasswordFileOpenFlags)