Skip to content

Commit

Permalink
Correct corruption of database by ValueSerializer #50
Browse files Browse the repository at this point in the history
  • Loading branch information
jorabin committed Aug 24, 2023
1 parent bc38b85 commit e2d7464
Showing 1 changed file with 5 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@

public class ValueSerializer extends StdSerializer<EntryClasses.StringProperty.Value>{

private StreamEncryptor encryptor;

public ValueSerializer() {
super(ValueSerializer.class, false);
}

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

public ValueSerializer(StreamEncryptor encryptor) {
super(ValueSerializer.class, false);
Expand All @@ -52,8 +44,9 @@ public void serialize(Value value, JsonGenerator gen, SerializerProvider provide
final ToXmlGenerator xmlGenerator = (ToXmlGenerator) gen;
xmlGenerator.writeStartObject();

String stringToWrite = value.getText();
//We need to encrypt and convert to base64 every protected element
if(value.getProtectOnOutput()) {
if (value.getProtectOnOutput()) {
xmlGenerator.setNextIsAttribute(true);
gen.writeStringField("Protected", "True");
String plain = value.getText();
Expand All @@ -63,17 +56,12 @@ public void serialize(Value value, JsonGenerator gen, SerializerProvider provide
//Cipher
byte[] encrypted = encryptor.encrypt(plain.getBytes());
//Convert to base64
String base64 = new String(Base64.encodeBase64(encrypted));

//Destroy from memory the plain value
plain = null;
value.setText(base64);

stringToWrite = new String(Base64.encodeBase64(encrypted));
}

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

gen.writeEndObject();

Expand Down

0 comments on commit e2d7464

Please sign in to comment.