Passwordfile library: io/passwordfile.h Source File

Passwordfile library 5.1.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
19 enum class PasswordFileOpenFlags : std::uint64_t {
20 None = 0,
21 ReadOnly = 1,
22 Default = None,
23};
24
25 PASSWORD_FILE_EXPORT std::string flagsToString(PasswordFileOpenFlags flags);
26
27 enum class PasswordFileSaveFlags : std::uint64_t {
28 None = 0,
29 Encryption = (1 << 0),
30 Compression = (1 << 1),
31 PasswordHashing = (1 << 2),
32 AllowToCreateNewFile = (1 << 3),
33 Default = Encryption | Compression | PasswordHashing | AllowToCreateNewFile,
34};
35
36 PASSWORD_FILE_EXPORT std::string flagsToString(PasswordFileSaveFlags flags);
37
38 class PASSWORD_FILE_EXPORT PasswordFile {
39public:
40 explicit PasswordFile();
41 explicit PasswordFile(const std::string &path, const std::string &password);
42 PasswordFile(const PasswordFile &other);
43 PasswordFile(PasswordFile &&other);
44 ~PasswordFile();
45 CppUtilities::NativeFileStream &fileStream();
46 void open(PasswordFileOpenFlags options = PasswordFileOpenFlags::Default);
47 void opened();
48 void generateRootEntry();
49 void create();
50 void close();
51 void load();
52 std::uint32_t mininumVersion(PasswordFileSaveFlags options) const;
53 void save(PasswordFileSaveFlags options = PasswordFileSaveFlags::Default);
54 void write(PasswordFileSaveFlags options = PasswordFileSaveFlags::Default);
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;
61 NodeEntry *rootEntry();
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;
77 PasswordFileOpenFlags openOptions() const;
78 PasswordFileSaveFlags saveOptions() 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
98 inline CppUtilities::NativeFileStream &PasswordFile::fileStream()
99{
100 return m_file;
101}
102
106 inline const std::string &PasswordFile::path() const
107{
108 return m_path;
109}
110
114 inline void PasswordFile::clearPath()
115{
116 close();
117 m_path.clear();
118}
119
123 inline const std::string &PasswordFile::password() const
124{
125 return m_password;
126}
127
131 inline 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
147 inline void PasswordFile::clearPassword()
148{
149 m_password.clear();
150}
151
155 inline bool PasswordFile::isOpen() const
156{
157 return m_file.is_open();
158}
159
163 inline std::string &PasswordFile::extendedHeader()
164{
165 return m_extendedHeader;
166}
167
171 inline const std::string &PasswordFile::extendedHeader() const
172{
173 return m_extendedHeader;
174}
175
179 inline std::string &PasswordFile::encryptedExtendedHeader()
180{
181 return m_encryptedExtendedHeader;
182}
183
187 inline const std::string &PasswordFile::encryptedExtendedHeader() const
188{
189 return m_encryptedExtendedHeader;
190}
191
196 inline std::uint32_t PasswordFile::version() const
197{
198 return m_version;
199}
200
204 inline PasswordFileOpenFlags PasswordFile::openOptions() const
205{
206 return m_openOptions;
207}
208
212 inline PasswordFileSaveFlags PasswordFile::saveOptions() const
213{
214 return m_saveOptions;
215}
216
217} // namespace Io
218
219CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Io, Io::PasswordFileOpenFlags);
220 CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Io, Io::PasswordFileSaveFlags);
221
222#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:204
Io::PasswordFile::rootEntry
const NodeEntry * rootEntry() const
Returns the root entry if present or nullptr otherwise.
Definition passwordfile.cpp:685
Io::PasswordFile::isOpen
bool isOpen() const
Returns an indication whether the file is open.
Definition passwordfile.h:155
Io::PasswordFile::clear
void clear()
Closes the file if opened.
Definition passwordfile.cpp:596
Io::PasswordFile::opened
void opened()
Handles the file being opened.
Definition passwordfile.cpp:136
Io::PasswordFile::clearEntries
void clearEntries()
Removes the root element if one is present.
Definition passwordfile.cpp:588
Io::PasswordFile::open
void open(PasswordFileOpenFlags options=PasswordFileOpenFlags::Default)
Opens the file.
Definition passwordfile.cpp:119
Io::PasswordFile::hasRootEntry
bool hasRootEntry() const
Returns an indication whether a root entry is present.
Definition passwordfile.cpp:677
Io::PasswordFile::create
void create()
Creates the file.
Definition passwordfile.cpp:160
Io::PasswordFile::exportToTextfile
void exportToTextfile(const std::string &targetPath) const
Writes the current root entry to a plain text file.
Definition passwordfile.cpp:612
Io::PasswordFile::load
void load()
Reads the contents of the file.
Definition passwordfile.cpp:177
Io::PasswordFile::close
void close()
Closes the file if currently opened.
Definition passwordfile.cpp:701
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:196
Io::PasswordFile::fileStream
CppUtilities::NativeFileStream & fileStream()
Returns the underlying file stream.
Definition passwordfile.h:98
Io::PasswordFile::clearPassword
void clearPassword()
Clears the current password.
Definition passwordfile.h:147
Io::PasswordFile::summary
std::string summary(PasswordFileSaveFlags saveOptions) const
Returns a summary about the file (version, used features, statistics).
Definition passwordfile.cpp:766
Io::PasswordFile::PasswordFile
PasswordFile()
Constructs a new password file.
Definition passwordfile.cpp:46
Io::PasswordFile::password
const std::string & password() const
Returns the current password.
Definition passwordfile.h:123
Io::PasswordFile::setPassword
void setPassword(const std::string &password)
Sets the current password.
Definition passwordfile.h:131
Io::PasswordFile::path
const std::string & path() const
Returns the current file path.
Definition passwordfile.h:106
Io::PasswordFile::isEncryptionUsed
bool isEncryptionUsed()
Returns an indication whether encryption is used and the file is open; returns always false otherwise...
Definition passwordfile.cpp:728
Io::PasswordFile::size
std::size_t size()
Returns the size of the file if the file is open; otherwise returns zero.
Definition passwordfile.cpp:754
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:399
Io::PasswordFile::encryptedExtendedHeader
std::string & encryptedExtendedHeader()
Returns the encrypted extended header.
Definition passwordfile.h:179
Io::PasswordFile::setPath
void setPath(const std::string &value)
Sets the current file path.
Definition passwordfile.cpp:712
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:379
Io::PasswordFile::extendedHeader
std::string & extendedHeader()
Returns the extended header.
Definition passwordfile.h:163
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:445
Io::PasswordFile::generateRootEntry
void generateRootEntry()
Generates a new root entry for the file.
Definition passwordfile.cpp:149
Io::PasswordFile::clearPath
void clearPath()
Clears the current path.
Definition passwordfile.h:114
Io::PasswordFile::doBackup
void doBackup()
Creates a backup of the file.
Definition passwordfile.cpp:654
Io::PasswordFile::saveOptions
PasswordFileSaveFlags saveOptions() const
Returns the save options used the last time when saving the file.
Definition passwordfile.h:212
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:790
Io::PasswordFileSaveFlags
PasswordFileSaveFlags
Definition passwordfile.h:27
Io::PasswordFileSaveFlags::Default
@ Default
Definition passwordfile.h:33
Io::PasswordFileSaveFlags::Compression
@ Compression
Definition passwordfile.h:30
Io::PasswordFileSaveFlags::AllowToCreateNewFile
@ AllowToCreateNewFile
Definition passwordfile.h:32
Io::PasswordFileSaveFlags::PasswordHashing
@ PasswordHashing
Definition passwordfile.h:31
Io::PasswordFileSaveFlags::Encryption
@ Encryption
Definition passwordfile.h:29
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:22
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Io, Io::PasswordFileOpenFlags)