Passwordfile library: io/passwordfile.h Source File

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
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 | AuthenticationTag,
36};
37
38 PASSWORD_FILE_EXPORT std::string flagsToString(PasswordFileSaveFlags flags);
39
40 class PASSWORD_FILE_EXPORT PasswordFile {
41public:
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();
48 void open(PasswordFileOpenFlags options = PasswordFileOpenFlags::Default);
49 void opened();
50 void generateRootEntry();
51 void create();
52 void close();
53 void load();
54 std::uint32_t mininumVersion(PasswordFileSaveFlags options) const;
55 void save(PasswordFileSaveFlags options = PasswordFileSaveFlags::Default);
56 void write(PasswordFileSaveFlags options = PasswordFileSaveFlags::Default);
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
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
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
116 inline void PasswordFile::clearPath()
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
141inline void PasswordFile::setPassword(const char *password, const size_t passwordSize)
142{
143 m_password.assign(password, passwordSize);
144}
145
149 inline void PasswordFile::clearPassword()
150{
151 m_password.clear();
152}
153
157 inline bool PasswordFile::isOpen() const
158{
159 return m_file.is_open();
160}
161
168 inline std::string &PasswordFile::extendedHeader()
169{
170 return m_extendedHeader;
171}
172
176 inline const std::string &PasswordFile::extendedHeader() const
177{
178 return m_extendedHeader;
179}
180
184 inline std::string &PasswordFile::encryptedExtendedHeader()
185{
186 return m_encryptedExtendedHeader;
187}
188
192 inline const std::string &PasswordFile::encryptedExtendedHeader() const
193{
194 return m_encryptedExtendedHeader;
195}
196
201 inline std::uint32_t PasswordFile::version() const
202{
203 return m_version;
204}
205
209 inline PasswordFileOpenFlags PasswordFile::openOptions() const
210{
211 return m_openOptions;
212}
213
217 inline PasswordFileSaveFlags PasswordFile::saveOptions() const
218{
219 return m_saveOptions;
220}
221
222} // namespace Io
223
224CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Io, Io::PasswordFileOpenFlags);
225 CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Io, Io::PasswordFileSaveFlags);
226
227#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:209
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:157
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:201
Io::PasswordFile::fileStream
CppUtilities::NativeFileStream & fileStream()
Returns the underlying file stream.
Definition passwordfile.h:100
Io::PasswordFile::clearPassword
void clearPassword()
Clears the current password.
Definition passwordfile.h:149
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:125
Io::PasswordFile::setPassword
void setPassword(const std::string &password)
Sets the current password.
Definition passwordfile.h:133
Io::PasswordFile::path
const std::string & path() const
Returns the current file path.
Definition passwordfile.h:108
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:184
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:168
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:116
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:217
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::Compression
@ Compression
Definition passwordfile.h:31
Io::PasswordFileSaveFlags::AllowToCreateNewFile
@ AllowToCreateNewFile
Definition passwordfile.h:33
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::New
@ New
Definition passwordfile.h:22
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)