2018-06-09 22:30:07 +02:00
|
|
|
#ifndef PASSWORD_FILE_IO_PASSWORD_FILE_H
|
|
|
|
#define PASSWORD_FILE_IO_PASSWORD_FILE_H
|
2016-08-29 15:42:07 +02:00
|
|
|
|
|
|
|
#include "../global.h"
|
|
|
|
|
|
|
|
#include <c++utilities/io/binaryreader.h>
|
|
|
|
#include <c++utilities/io/binarywriter.h>
|
2018-09-05 00:29:36 +02:00
|
|
|
#include <c++utilities/io/nativefilestream.h>
|
2016-08-29 15:42:07 +02:00
|
|
|
|
|
|
|
#include <fstream>
|
2017-05-01 03:25:30 +02:00
|
|
|
#include <iostream>
|
2016-08-29 15:42:07 +02:00
|
|
|
#include <memory>
|
2017-05-01 03:25:30 +02:00
|
|
|
#include <string>
|
2016-08-29 15:42:07 +02:00
|
|
|
|
|
|
|
namespace Io {
|
|
|
|
|
|
|
|
class NodeEntry;
|
|
|
|
|
2018-12-18 23:17:19 +01:00
|
|
|
enum class PasswordFileOpenFlags : uint64 {
|
|
|
|
None = 0,
|
|
|
|
ReadOnly = 1,
|
|
|
|
Default = None,
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr PasswordFileOpenFlags operator|(PasswordFileOpenFlags lhs, PasswordFileOpenFlags rhs)
|
|
|
|
{
|
|
|
|
return static_cast<PasswordFileOpenFlags>(
|
|
|
|
static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(lhs) | static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(rhs));
|
|
|
|
}
|
|
|
|
|
2018-12-19 00:14:59 +01:00
|
|
|
constexpr PasswordFileOpenFlags &operator|=(PasswordFileOpenFlags &lhs, PasswordFileOpenFlags rhs)
|
2018-12-18 23:17:19 +01:00
|
|
|
{
|
2018-12-19 00:14:59 +01:00
|
|
|
return lhs = static_cast<PasswordFileOpenFlags>(static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(lhs)
|
|
|
|
| static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(rhs));
|
2018-12-18 23:17:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool operator&(PasswordFileOpenFlags lhs, PasswordFileOpenFlags rhs)
|
|
|
|
{
|
|
|
|
return static_cast<bool>(
|
|
|
|
static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(lhs) & static_cast<std::underlying_type<PasswordFileOpenFlags>::type>(rhs));
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class PasswordFileSaveFlags : uint64 {
|
|
|
|
None = 0,
|
|
|
|
Encryption = 1,
|
|
|
|
Compression = 2,
|
|
|
|
PasswordHashing = 4,
|
|
|
|
Default = Encryption | Compression | PasswordHashing,
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr PasswordFileSaveFlags operator|(PasswordFileSaveFlags lhs, PasswordFileSaveFlags rhs)
|
|
|
|
{
|
|
|
|
return static_cast<PasswordFileSaveFlags>(
|
|
|
|
static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(lhs) | static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(rhs));
|
|
|
|
}
|
|
|
|
|
2018-12-19 00:14:59 +01:00
|
|
|
constexpr PasswordFileSaveFlags &operator|=(PasswordFileSaveFlags &lhs, PasswordFileSaveFlags rhs)
|
2018-12-18 23:17:19 +01:00
|
|
|
{
|
2018-12-19 00:14:59 +01:00
|
|
|
return lhs = static_cast<PasswordFileSaveFlags>(static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(lhs)
|
|
|
|
| static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(rhs));
|
2018-12-18 23:17:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool operator&(PasswordFileSaveFlags lhs, PasswordFileSaveFlags rhs)
|
|
|
|
{
|
|
|
|
return static_cast<bool>(
|
|
|
|
static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(lhs) & static_cast<std::underlying_type<PasswordFileSaveFlags>::type>(rhs));
|
|
|
|
}
|
|
|
|
|
2017-05-01 03:25:30 +02:00
|
|
|
class PASSWORD_FILE_EXPORT PasswordFile {
|
2016-08-29 15:42:07 +02:00
|
|
|
public:
|
|
|
|
explicit PasswordFile();
|
|
|
|
explicit PasswordFile(const std::string &path, const std::string &password);
|
|
|
|
PasswordFile(const PasswordFile &other);
|
2018-09-05 00:32:32 +02:00
|
|
|
PasswordFile(PasswordFile &&other);
|
2016-08-29 15:42:07 +02:00
|
|
|
~PasswordFile();
|
2018-09-05 00:33:01 +02:00
|
|
|
IoUtilities::NativeFileStream &fileStream();
|
2018-12-18 23:17:19 +01:00
|
|
|
void open(PasswordFileOpenFlags options = PasswordFileOpenFlags::Default);
|
2018-09-05 00:33:01 +02:00
|
|
|
void opened();
|
2016-08-29 15:42:07 +02:00
|
|
|
void generateRootEntry();
|
|
|
|
void create();
|
|
|
|
void close();
|
|
|
|
void load();
|
2018-12-18 23:17:19 +01:00
|
|
|
uint32 mininumVersion(PasswordFileSaveFlags options) const;
|
|
|
|
void save(PasswordFileSaveFlags options = PasswordFileSaveFlags::Default);
|
|
|
|
void write(PasswordFileSaveFlags options = PasswordFileSaveFlags::Default);
|
2016-08-29 15:42:07 +02:00
|
|
|
void clearEntries();
|
|
|
|
void clear();
|
|
|
|
void exportToTextfile(const std::string &targetPath) const;
|
|
|
|
void doBackup();
|
|
|
|
bool hasRootEntry() const;
|
|
|
|
const NodeEntry *rootEntry() const;
|
|
|
|
NodeEntry *rootEntry();
|
|
|
|
const std::string &path() const;
|
2018-12-18 23:17:19 +01:00
|
|
|
const std::string &password() const;
|
2016-08-29 15:42:07 +02:00
|
|
|
void setPath(const std::string &value);
|
|
|
|
void clearPath();
|
2018-12-18 23:17:19 +01:00
|
|
|
void setPassword(const std::string &password);
|
|
|
|
void setPassword(const char *password, const std::size_t passwordSize);
|
2016-08-29 15:42:07 +02:00
|
|
|
void clearPassword();
|
|
|
|
bool isEncryptionUsed();
|
|
|
|
bool isOpen() const;
|
2018-12-18 23:17:19 +01:00
|
|
|
std::string &extendedHeader();
|
|
|
|
const std::string &extendedHeader() const;
|
|
|
|
std::string &encryptedExtendedHeader();
|
|
|
|
const std::string &encryptedExtendedHeader() const;
|
2018-06-12 21:49:16 +02:00
|
|
|
std::size_t size();
|
2017-05-01 03:25:30 +02:00
|
|
|
|
2016-08-29 15:42:07 +02:00
|
|
|
private:
|
|
|
|
std::string m_path;
|
2018-12-18 23:17:19 +01:00
|
|
|
std::string m_password;
|
2016-08-29 15:42:07 +02:00
|
|
|
std::unique_ptr<NodeEntry> m_rootEntry;
|
|
|
|
std::string m_extendedHeader;
|
|
|
|
std::string m_encryptedExtendedHeader;
|
2018-09-05 00:29:36 +02:00
|
|
|
IoUtilities::NativeFileStream m_file;
|
2016-08-29 15:42:07 +02:00
|
|
|
IoUtilities::BinaryReader m_freader;
|
|
|
|
IoUtilities::BinaryWriter m_fwriter;
|
|
|
|
};
|
2018-09-05 00:33:01 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns the underlying file stream.
|
|
|
|
*/
|
|
|
|
inline IoUtilities::NativeFileStream &PasswordFile::fileStream()
|
|
|
|
{
|
|
|
|
return m_file;
|
|
|
|
}
|
|
|
|
|
2018-03-20 20:11:31 +01:00
|
|
|
} // namespace Io
|
2016-08-29 15:42:07 +02:00
|
|
|
|
2018-06-09 22:30:07 +02:00
|
|
|
#endif // PASSWORD_FILE_IO_PASSWORD_FILE_H
|