Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Usage sample of PropertyValue from an Entry #80

Ramel started this conversation in Show and tell
Discussion options

As I don't find any sample of the concept of geting a PropertyValue from an Entry, I've write a test. Don't known if it's correct and implemented correctly the concept, but it seems to work.

package com.testdbx;
import org.linguafranca.pwdb.kdbx.jackson.JacksonDatabase;
import org.linguafranca.pwdb.kdbx.KdbxCredentials;
import org.linguafranca.pwdb.Entry;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.linguafranca.pwdb.PropertyValue;
import org.linguafranca.pwdb.kdbx.jackson.JacksonEntry;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.List;
// Reading the https://github.com/jorabin/KeePassJava2/blob/master/PropertyValueProtection.md
// Test of getting of PropertyValue from an Entry.
// mvn clean compile exec:java -Dexec.mainClass="com.testdbx.KdbxOpener" -Dexec.args="C:/path/to/the/database.kdbx password"
public class KdbxOpener {
 public static void main(String[] args) {
 String databasePath = args[0]; //"C:/path/to/the/database.kdbx";
 String password = args[1];
		//String newPassword = args[2]; // test to later modify password
		System.out.println("databasePath = " + databasePath);
 JacksonDatabase database = openDatabase(databasePath, password);
 if (database != null) {
 //serializeToJson(database);
			getEntryByTitle(database, "MY_ENTRY");
			//setEntryPassword(database, "MY_ENTRY", newPassword);
 //saveDatabase(database, databasePath, password);
 }
 }
 private static JacksonDatabase openDatabase(String databasePath, String password) {
 try (InputStream inputStream = new FileInputStream(new File(databasePath))) {
 KdbxCredentials.Password credentials = new KdbxCredentials.Password(password.getBytes());
 JacksonDatabase database = JacksonDatabase.load(credentials, inputStream);
 System.out.println("Database opened successfully.");
 return database;
 } catch (Exception e) {
 e.printStackTrace();
 System.err.println("Failed to open the database.");
 return null;
 }
 }
 private static void serializeToJson(JacksonDatabase database) {
 ObjectMapper objectMapper = new ObjectMapper();
 try {
 String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(database);
 System.out.println("Database serialized to JSON:");
 System.out.println(json);
 } catch (IOException e) {
 e.printStackTrace();
 System.err.println("Failed to serialize the database to JSON.");
 }
 }
	
	private static JacksonEntry getEntryByTitle(JacksonDatabase database, String title) {
 List<? extends JacksonEntry> entries = database.findEntries(title);
 System.out.println("Number of entries found: " + entries.size());
 if (entries.isEmpty()) {
 System.out.println("No entry found with title: " + title);
 return null;
 }
		JacksonEntry entry = entries.get(0);
 PropertyValue passwordValue = entry.getPropertyValue("Password");
 if (passwordValue != null) {
 System.out.println("Password: " + passwordValue.getValueAsString());
 }
 return entry;
 }
	
 private static void setEntryPassword(JacksonDatabase database, String title, String newPassword) {
 List<? extends JacksonEntry> entries = database.findEntries(title);
 System.out.println("Number of entries found: " + entries.size());
 if (entries.isEmpty()) {
 System.out.println("No entry found with title: " + title);
 return;
 }
 JacksonEntry entry = entries.get(0);
 PropertyValue passwordValue = entry.getPropertyValue("Password");
 if (passwordValue != null) {
 System.out.println("Password: " + passwordValue.getValueAsString());
 }
 PropertyValue newPasswordValue = new PropertyValue() {
 @Override
 public String getValueAsString() {
 return newPassword;
 }
 @Override
 public boolean isProtected() {
 return false;
 }
 @Override
 public byte[] getValueAsBytes() {
 return newPassword.getBytes();
 }
 @Override
 public char[] getValueAsChars() {
 return newPassword.toCharArray();
 }
 @Override
 public CharSequence getValue() {
 return newPassword;
 }
 };
 entry.setPropertyValue("Password", newPasswordValue);
 System.out.println("New Password set.");
 }
 private static void saveDatabase(JacksonDatabase database, String databasePath, String password) {
 try (OutputStream outputStream = new FileOutputStream(new File(databasePath))) {
 KdbxCredentials.Password credentials = new KdbxCredentials.Password(password.getBytes());
 database.save(credentials, outputStream);
 System.out.println("Database saved successfully.");
 } catch (Exception e) {
 e.printStackTrace();
 System.err.println("Failed to save the database.");
 }
 }
}
You must be logged in to vote

Replies: 1 comment

Comment options

Hello @Ramel

I agree that it's not clearly enough explained, nor are there good examples. So I wrote a simple illustration that I think looks like what you are trying to do.

Hopefully that clarifies.

It's also worth noting that I'm introducing a lot of simplifications for version 3.0.0. If you compare the above illustration (for 2.2.4) with this for version 3.0.0 I think you'll see it's a bit cleaner and simpler to use.

Let me know
thanks
Jo

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /