libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
keepass::KeePass Class Referencefinal

High-level entry point for opening and saving KeePass databases. More...

#include <keepass.hh>

Public Types

enum class  Format { kAuto , kKdb , kKdbx3 , kKdbx4 }
 Database formats supported for reading and writing. More...
 

Public Member Functions

 KeePass ()
 Constructs an importer/exporter with an empty key.
 
 KeePass (const Key &key)
 Constructs an importer/exporter with the given key.
 
 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::string &path)
 Opens a KeePass (KDB or KDBX) database file.
 
std::unique_ptr< Database > Open (std::istream &src)
 Opens a KeePass database from a stream with format auto-detection.
 
std::unique_ptr< Database > Open (const uint8_t *data, std::size_t size)
 Opens a KeePass database from an in-memory byte range.
 
std::unique_ptr< Database > Open (const std::vector< uint8_t > &data)
 Opens a KeePass database from an in-memory byte vector.
 
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.
 
void Save (std::ostream &dst, const Database &db, const Key &key)
 Saves a database to a stream using a new key.
 
void SaveAs (const std::string &path, const Database &db, const Key &new_key)
 Saves a database to a file using a new key.
 
void SaveAs (std::ostream &dst, const Database &db, const Key &new_key)
 Saves a database to a stream using a new key.
 
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.
 
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 SetFormat (Format format)
 Selects the output format.
 
Format GetFormat () const
 Returns the configured output format.
 

Static Public Member Functions

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.
 

Detailed Description

High-level entry point for opening and saving KeePass databases.

KeePass unifies the KDB and KDBX importers/exporters behind a single interface. The format is auto-detected on input; on output it can either be selected explicitly via SetFormat or derived from the database content and the target file extension.

Member Enumeration Documentation

◆ Format

Database formats supported for reading and writing.

Enumerator
kAuto 

Auto-detect the format.

kKdb 

KeePass 1.x KDB format.

kKdbx3 

KeePass 2.x KDBX 3 format.

kKdbx4 

KeePass 2.x KDBX 4 format.

Constructor & Destructor Documentation

◆ KeePass() [1/2]

keepass::KeePass::KeePass ( const Key &  key)
explicit

Constructs an importer/exporter with the given key.

Parameters
keyThe key used to decrypt and encrypt databases.

◆ KeePass() [2/2]

keepass::KeePass::KeePass ( const std::string &  password,
const std::string &  keyfile = "" 
)

Constructs an importer/exporter from a password and optional key file.

Parameters
passwordThe database password.
keyfilePath to a key file, or an empty string for none.

Member Function Documentation

◆ Create()

static std::unique_ptr< Database > keepass::KeePass::Create ( const std::string &  password = "",
Format  format = Format::kKdbx4,
Database::Cipher  cipher = Database::Cipher::kAes,
Database::Kdf  kdf = Database::Kdf::kArgon2id 
)
static

Creates a new empty database with generated cryptographic material.

When password is non-empty and format is not KDB, the transformed key is derived immediately and cached on the returned Database. A later KDBX Save then works with an empty KeePass key (or use SaveAs with a new key to re-encrypt). KDB (1.x) export still requires an explicit key because the legacy format derives its key with different parameters.

Parameters
passwordPassword to derive and cache the transformed key for.
formatPreferred output format of the new database.
cipherPayload cipher to use.
kdfKey derivation function to use.
Returns
A unique pointer to the newly created Database object.

◆ Open() [1/3]

std::unique_ptr< Database > keepass::KeePass::Open ( const std::string &  path)

Opens a KeePass (KDB or KDBX) database file.

Parameters
pathPath to the database file on disk.
Returns
A unique pointer to the imported Database object.

◆ Open() [2/3]

std::unique_ptr< Database > keepass::KeePass::Open ( const uint8_t *  data,
std::size_t  size 
)

Opens a KeePass database from an in-memory byte range.

The bytes are copied into a seekable stream; no reference is retained after the call. This is a convenience overload for callers that already hold the database in memory (for example after a network transfer).

Parameters
dataPointer to the database bytes.
sizeNumber of bytes in data.
Returns
A unique pointer to the imported Database object.

◆ Open() [3/3]

std::unique_ptr< Database > keepass::KeePass::Open ( std::istream &  src)

Opens a KeePass database from a stream with format auto-detection.

Seekable streams are read directly (only the 8-byte format signature is inspected up front); non-seekable streams are buffered in memory.

Parameters
srcThe input stream containing the database.
Returns
A unique pointer to the imported Database object.

◆ Save() [1/3]

void keepass::KeePass::Save ( const std::string &  path,
const Database &  db 
)

Saves a database to a file.

The output format is taken from SetFormat if set, otherwise derived from the file extension and the database content.

Parameters
pathPath to the output file.
dbThe database to save.

◆ Save() [2/3]

void keepass::KeePass::Save ( std::ostream &  dst,
const Database &  db 
)

Saves a database to a stream.

The output format is taken from SetFormat if set, otherwise a KDBX format is chosen based on the database KDF and cipher.

Parameters
dstThe output stream.
dbThe database to save.

◆ Save() [3/3]

void keepass::KeePass::Save ( std::ostream &  dst,
const Database &  db,
const Key &  key 
)

Saves a database to a stream using a new key.

The key is used only for this save and key_ is restored afterwards. The output format is taken from SetFormat if set, otherwise a KDBX format is chosen based on the database KDF and cipher. KDB cannot be selected for stream output because there is no file extension.

Parameters
dstThe output stream.
dbThe database to save.
keyThe key to encrypt the database with.

◆ SaveAs() [1/4]

void keepass::KeePass::SaveAs ( const std::string &  path,
const Database &  db,
const Key &  new_key 
)

Saves a database to a file using a new key.

The output format is taken from SetFormat if set, otherwise a KDBX format is chosen based on the database KDF and cipher. KDB cannot be selected for stream output because there is no file extension.

Parameters
pathPath to the output file.
dbThe database to save.
new_keyThe key to encrypt the database with.

◆ SaveAs() [2/4]

void keepass::KeePass::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.

Parameters
pathPath to the output file.
dbThe database to save.
passwordThe new database password.
keyfilePath to a key file, or an empty string for none.

◆ SaveAs() [3/4]

void keepass::KeePass::SaveAs ( std::ostream &  dst,
const Database &  db,
const Key &  new_key 
)

Saves a database to a stream using a new key.

The output format is taken from SetFormat if set, otherwise a KDBX format is chosen based on the database KDF and cipher. KDB cannot be selected for stream output because there is no file extension.

Parameters
dstThe output stream.
dbThe database to save.
new_keyThe key to encrypt the database with.

◆ SaveAs() [4/4]

void keepass::KeePass::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.

The output format is taken from SetFormat if set, otherwise a KDBX format is chosen based on the database KDF and cipher. KDB cannot be selected for stream output because there is no file extension.

Parameters
dstThe output stream.
dbThe database to save.
passwordThe new database password.
keyfilePath to a key file, or an empty string for none.

The documentation for this class was generated from the following file: