Skip to content

Commit

Permalink
More tudying
Browse files Browse the repository at this point in the history
  • Loading branch information
jorabin committed Aug 25, 2023
1 parent d952a98 commit 99ce6ec
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ public String getDescription() {
return;
}
logger.info("Opening {}", fc.getSelectedFile().getPath());
/*
Util.listXml(fc.getSelectedFile().getName(),
new KdbxCreds(s.getBytes()),
new PrintWriter(outputStream));
*/
try (InputStream is = Files.newInputStream(Paths.get(fc.getSelectedFile().getPath()))) {
/* try (InputStream is = Files.newInputStream(Paths.get(fc.getSelectedFile().getPath()))) {
HexViewer.list(is);
}
}*/
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.linguafranca.pwdb.kdbx.jackson.converter;

import java.util.Date;
import java.util.Objects;

import org.linguafranca.pwdb.kdbx.Helpers;

Expand All @@ -25,17 +26,13 @@ public class StringToDateConverter extends StdConverter<String, Date> {

@Override
public Date convert(String value) {
Date result = null;
if(value != null) {
if(value.equals("${creationDate}")) {
result = new Date();
}
try {
result = Helpers.toDate(value);
} catch(Exception e) {
result = new Date();
}
// TODO: It would really be better if we could inhibit deserialize date elements that are not present
if (Objects.isNull(value) || value.isEmpty()) {
return null;
}
return result;
if(value.equals("${creationDate}")) {
return new Date();
}
return Helpers.toDate(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,28 @@

public class ValueDeserializer extends StdDeserializer<EntryClasses.StringProperty.Value> {

private StreamEncryptor encryptor;

public ValueDeserializer() {
super(ValueDeserializer.class);
}

public ValueDeserializer(Class<EntryClasses.StringProperty.Value> v) {
super(v);
}
private final StreamEncryptor encryptor;

public ValueDeserializer(StreamEncryptor encryptor) {
super(ValueDeserializer.class);
this.encryptor = encryptor;
}

@Override
public EntryClasses.StringProperty.Value deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {

public EntryClasses.StringProperty.Value deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {

JsonNode node = p.getCodec().readTree(p);

EntryClasses.StringProperty.Value result = new EntryClasses.StringProperty.Value();

if(node.isTextual()) {
result.setText(node.textValue());
} else if(node.isObject()) {

return result;
}
if(node.isObject()) {
// TODO not clear what is happening here, looks like it's not exactly correct
//We need to decrypt all Protected values
if(node.has("Protected")) {

//Check if Protected=True
Boolean nodeEncrypted = Helpers.toBoolean(node.get("Protected").asText());
if(nodeEncrypted) {
if(node.has("Protected") && Boolean.TRUE.equals(Helpers.toBoolean(node.get("Protected").asText()))) {
if(node.has("")) {
String cipherText = node.get("").asText();
if(cipherText != null && !cipherText.isEmpty()) {
Expand All @@ -75,7 +64,6 @@ public EntryClasses.StringProperty.Value deserialize(JsonParser p, Deserializati
result.setProtectOnOutput(true);
}
}
}
} else {
//If an element is not marked us Protected we need to copy the value as is
if(node.has("ProtectInMemory")) {
Expand All @@ -88,8 +76,6 @@ public EntryClasses.StringProperty.Value deserialize(JsonParser p, Deserializati
}
}
}

return result;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,20 @@ public void serialize(Value value, JsonGenerator gen, SerializerProvider provide
//We need to encrypt and convert to base64 every protected element
if (value.getProtectOnOutput()) {
xmlGenerator.setNextIsAttribute(true);
gen.writeStringField("Protected", "True");
xmlGenerator.writeStringField("Protected", "True");
String plain = value.getText();
if(plain == null) {
plain = "";
}
//Cipher
byte[] encrypted = encryptor.encrypt(plain.getBytes());
byte[] encrypted = encryptor.encrypt(plain.getBytes());
//Convert to base64
stringToWrite = new String(Base64.encodeBase64(encrypted));
}

xmlGenerator.setNextIsAttribute(false);
xmlGenerator.setNextIsUnwrapped(true);
xmlGenerator.writeStringField("text", stringToWrite);

gen.writeEndObject();

xmlGenerator.writeEndObject();
}

}

0 comments on commit 99ce6ec

Please sign in to comment.