A Rust library for reading and writing KeePass 2 and KeePassX databases.
To use rust-kpdb
, add the following to your Cargo.toml:
toml
[dependencies]
rust-kpdb = "0.3.0"
And the following to your crate root:
rust
extern crate kpdb;
Create a new database adding two groups and two entries:
```rust use kpdb::{CompositeKey, Database, Entry, Group};
// Create a new database. let key = CompositeKey::from_password("password"); let mut db = Database::new(&key);
// Create a new group named Email. let mut emailgroup = Group::new("Email"); let emailgroupuuid = emailgroup.uuid;
// Create an entry for ProtonMail and add it to the Email group. let mut protonmail = Entry::new(); let protonmailuuid = protonmail.uuid; protonmail.settitle("ProtonMail"); protonmail.setusername("mailuser"); protonmail.setpassword("mailpass"); protonmail.seturl("https://mail.protonmail.com"); emailgroup.add_entry(protonmail);
// Create a new group named VPN. let mut vpn_group = Group::new("VPN");
// Create an entry for ProtonVPN and add it to the VPN group. let mut protonvpn = Entry::new(); protonvpn.settitle("ProtonVPN"); protonvpn.setusername("vpnuser"); protonvpn.setpassword("vpnpass"); protonvpn.seturl("https://prontvpn.com"); vpngroup.addentry(protonvpn);
// Add the Email and VPN groups to the Root group. db.rootgroup.addgroup(emailgroup); db.rootgroup.addgroup(vpngroup);
// Find groups matching "email". let groups = db.findgroups("email"); asserteq!(groups.len(), 1);
// Find entries matching "proton". let entries = db.findentries("proton"); asserteq!(entries.len(), 2);
// Retrieve a group by its UUID. let group = db.getgroup(emailgroupuuid).unwrap(); asserteq!(group.name, "Email");
// Retrieve an entry by its UUID. let entry = db.getentry(protonmailuuid).unwrap(); asserteq!(entry.title(), Some("ProtonMail")); asserteq!(entry.username(), Some("mailuser")); asserteq!(entry.password(), Some("mailpass")); asserteq!(entry.url(), Some("https://mail.protonmail.com")); assert_eq!(entry.notes(), None); ```
Open the existing KeePass database passwords.kdbx using the password "password", print it and save it to new.kdbx:
```rust use kpdb::{CompositeKey, Database}; use std::fs::File;
let mut file = File::open("passwords.kdbx").unwrap(); let key = CompositeKey::from_password("password"); let db = Database::open(&mut file, &key).unwrap();
println!("{:?}", db);
let mut file = File::create("new.kdbx").unwrap(); db.save(&mut file).unwrap(); ```
Open the existing KeePass database passwords.kdbx using both the password "password" and the key file passwords.key, print it and save it to new.kdbx:
```rust use kpdb::{CompositeKey, Database, KeyFile}; use std::fs::File;
let mut file = File::open("passwords.key").unwrap(); let keyfile = KeyFile::open(&mut file).unwrap(); let key = CompositeKey::fromboth("password", key_file);
let mut file = File::open("passwords.kdbx").unwrap(); let db = Database::open(&mut file, &key).unwrap();
println!("{:?}", db);
let mut file = File::create("new.kdbx").unwrap(); db.save(&mut file).unwrap(); ```
The following features are currently not implemented:
Rust-kpdb is dual licensed under the MIT and Apache 2.0 licenses, the same licenses as the Rust compiler.
Contributions are welcome. By submitting a pull request you are agreeing to make you work available under the license terms of the Rust-kpdb project.