To read a .kdbx file, you need to create a new database instance, provide credentials, and use a decoder to populate the database object.
Important Security Note: After decoding, you must call db.UnlockProtectedEntries() to access protected fields (like passwords). Before saving the database back to a file, you must call db.LockProtectedEntries() to ensure passwords are not stored in plaintext in the XML.
Compatibility Note: While the library can read most KeePass v2 files, it is recommended to use KeePass v2.30 or higher. Files created with this library are expected to be read by KeePass v2.30+.
package main
import (
"fmt"
"github.com/tobischo/gokeepasslib/v3"
"os"
)
func main() {
file, _ := os.Open("examples/reading/example.kdbx")
db := gokeepasslib.NewDatabase()
db.Credentials = gokeepasslib.NewPasswordCredentials("abcdefg12345678")
_ = gokeepasslib.NewDecoder(file).Decode(db)
// Unlock protected entries to access sensitive data
db.UnlockProtectedEntries()
// Accessing data (ensure you perform bounds checking in production)
entry := db.Content.Root.Groups[0].Groups[0].Entries[0]
fmt.Println(entry.GetTitle())
fmt.Println(entry.GetPassword())
}