Fork me on GitHub

Programming, Internet & more

Openkeepass: Feature release with write support

Openkeepass is around for a while now and has proved to be a major player when it comes to reading of KeePass files (especially KeePass 2.x).

However, since its initial release I’ve received a lot of requests regarding write support of KeePass files. After a lot of work this is finally done and has found its way into a new feature release of openkeepass.

Writing KeePass files

The major feature of version 0.5.0 is to support writing of KeePass files. To simply create a new KeePass file from scratch you can use the fluent API of the builders. A very simple example to write a KeePass file with only one single entry would be this:

// Create an entry
Entry entryOne = new EntryBuilder("First entry")
.username("Carl")
.password("Carls secret")
.build();

// Create the database file
KeePassFile keePassFile = new KeePassFileBuilder("myNewKeePassFile")
.addTopEntries(entryOne)
.build();

// Write database file to disk
KeePassDatabase.write(keePassFile, "masterPassword", "myNewKeePassFile.kdbx");

By nesting the available builders it is also possible to create much more complex structures in the KeePass file.

The code to create this structure looks like this:

// Create the more complex tree structure
Group root = new GroupBuilder("TestDb")
            .addEntry(new EntryBuilder("First entry").build())
            .addGroup(new GroupBuilder("Banking").build())
            .addGroup(new GroupBuilder("Internet")
                    .addGroup(new GroupBuilder("Shopping")
                            .addEntry(new EntryBuilder("Second entry").build())
                            .build())
                    .build())
            .build();

// Create the database file    
KeePassFile keePassFile = new KeePassFileBuilder("TestDb")
        .addTopGroups(root)
        .build();

// Write database file to disk     
KeePassDatabase.write(keePassFile, "masterPassword", "myNewKeePassFile.kdbx");

Modify existing KeePass files

There is also a new concept called zipper which comes into play when a KeePass structure should be modified instead of creating a new one from scratch. The zipper can be used to easily navigate through the tree structure of a KeePass file and can be compared with the concept of an iterator. There is always a pointer to an element in the tree and by navigating through the tree, the pointer to the current element will be shifted around.

This is very helpful if you want to replace some nodes in the tree. Please note that entries cannot be replaced directly in the tree, you have to modify the parent group of an entry if you want to modify or replace an entry.

The following example shows how to rename an existing group by using the zipper:

// Open keepass file
KeePassFile database = KeePassDatabase.getInstance("database.kdbx").openDatabase("password");
       
// Navigate through tree to group
GroupZipper zipper = new KeePassFileBuilder(database).getZipper()
            .down()
            .right();
       
// Rename group
Group group = zipper.getNode();
Group modifiedGroup = new GroupBuilder(group).name("test2").build();
       
// Replace old group with new one
KeePassFile modifiedDatabase = zipper.replace(modifiedGroup).close();

// Write database file to disk
KeePassDatabase.write(modifiedDatabase, "password", "modifiedDatabase.kdbx");

Where can I get it

You can download it with maven or directly from GitHub.

<dependency>
    <groupId>de.slackspace</groupId>
    <artifactId>openkeepass</artifactId>
    <version>0.5.0</version>
</dependency>
Category: open-source

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *