libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
keepass.hh
Go to the documentation of this file.
1/*
2 * libkeepass - KeePass key database importer/exporter
3 * Copyright (C) 2014 Christian Kindahl
4 * Copyright (C) 2024 Dominik Krümpelmann
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
25#pragma once
26#include <cstddef>
27#include <cstdint>
28#include <istream>
29#include <memory>
30#include <ostream>
31#include <string>
32#include <vector>
33
34#include "database.hh"
35#include "key.hh"
36#include "libkeepass/export.hh"
37
38namespace keepass {
39
40class KdbFile;
41
51public:
53 enum class Format {
54 kAuto,
55 kKdb,
56 kKdbx3,
57 kKdbx4
58 };
59
62
64
67 explicit KeePass(const Key& key);
68
70
74 KeePass(const std::string& password, const std::string& keyfile = "");
75
77
81 std::unique_ptr<Database> Open(const std::string& path);
82
84
91 std::unique_ptr<Database> Open(std::istream& src);
92
94
103 std::unique_ptr<Database> Open(const uint8_t* data, std::size_t size);
104
106 std::unique_ptr<Database> Open(const std::vector<uint8_t>& data) {
107 return Open(data.data(), data.size());
108 }
109
111
118 void Save(const std::string& path, const Database& db);
119
121
128 void Save(std::ostream& dst, const Database& db);
129
131
141 void Save(std::ostream& dst, const Database& db, const Key& key);
142
144
153 void SaveAs(const std::string& path, const Database& db, const Key& new_key);
154
156
165 void SaveAs(std::ostream& dst, const Database& db, const Key& new_key);
166
168
174 void SaveAs(const std::string& path, const Database& db, const std::string& password,
175 const std::string& keyfile = "");
176
178
188 void SaveAs(std::ostream& dst, const Database& db, const std::string& password,
189 const std::string& keyfile = "");
190
192 void SetFormat(Format format) { format_ = format; }
193
195 Format GetFormat() const { return format_; }
196
198
211 static std::unique_ptr<Database> Create(const std::string& password = "",
212 Format format = Format::kKdbx4,
213 Database::Cipher cipher = Database::Cipher::kAes,
214 Database::Kdf kdf = Database::Kdf::kArgon2id);
215
216private:
217 std::unique_ptr<Database> Import(std::istream& src, Format format);
218 void Save(std::ostream& dst, const Database& db, Format format);
219 static void SaveWithOptions(std::ostream& dst, const Database& db, const Key& key, Format format,
220 bool use_cached_key);
221 Format ResolveOutputFormat(const std::string& path, const Database& db) const;
222 Format ResolveStreamFormat(const Database& db) const;
223
224 Key key_;
225 Format format_ = Format::kAuto;
226};
227
228} // namespace keepass
Streams values into a built-in string builder.
Definition format.hh:32
Represents a KeePass (KDB/KDBX) database.
Definition database.hh:74
Kdf
Key derivation functions supported by the importer/exporter.
Definition database.hh:80
Cipher
Cipher algorithms supported for encrypting the database payload.
Definition database.hh:77
High-level entry point for opening and saving KeePass databases.
Definition keepass.hh:50
Format GetFormat() const
Returns the configured output format.
Definition keepass.hh:195
void SaveAs(std::ostream &dst, const Database &db, const std::string &password, const std::string &keyfile="")
Saves a database to a stream using a new password and optional key file.
void SaveAs(const std::string &path, const Database &db, const std::string &password, const std::string &keyfile="")
Saves a database to a file using a new password and optional key file.
KeePass(const Key &key)
Constructs an importer/exporter with the given key.
void Save(const std::string &path, const Database &db)
Saves a database to a file.
void Save(std::ostream &dst, const Database &db)
Saves a database to a stream.
std::unique_ptr< Database > Open(std::istream &src)
Opens a KeePass database from a stream with format auto-detection.
void SetFormat(Format format)
Selects the output format.
Definition keepass.hh:192
void Save(std::ostream &dst, const Database &db, const Key &key)
Saves a database to a stream using a new key.
std::unique_ptr< Database > Open(const uint8_t *data, std::size_t size)
Opens a KeePass database from an in-memory byte range.
void SaveAs(std::ostream &dst, const Database &db, const Key &new_key)
Saves a database to a stream using a new key.
KeePass()
Constructs an importer/exporter with an empty key.
void SaveAs(const std::string &path, const Database &db, const Key &new_key)
Saves a database to a file using a new key.
std::unique_ptr< Database > Open(const std::string &path)
Opens a KeePass (KDB or KDBX) database file.
KeePass(const std::string &password, const std::string &keyfile="")
Constructs an importer/exporter from a password and optional key file.
std::unique_ptr< Database > Open(const std::vector< uint8_t > &data)
Opens a KeePass database from an in-memory byte vector.
Definition keepass.hh:106
static std::unique_ptr< Database > Create(const std::string &password="", Format format=Format::kKdbx4, Database::Cipher cipher=Database::Cipher::kAes, Database::Kdf kdf=Database::Kdf::kArgon2id)
Creates a new empty database with generated cryptographic material.
Manages database encryption keys for KDB and KDBX formats.
Definition key.hh:45
In-memory representation of a KeePass database.
Visibility/export macro for the shared library.
#define LIBKEEPASS_API
Marks a class, function or variable as part of the public ABI.
Definition export.hh:43
Database encryption key management for KDB and KDBX formats.