That fuck shit the fascists are using
at master 116 lines 3.2 kB view raw
1package org.tm.archive.crypto; 2 3 4import android.util.Base64; 5 6import androidx.annotation.NonNull; 7 8import com.fasterxml.jackson.annotation.JsonIgnore; 9import com.fasterxml.jackson.annotation.JsonProperty; 10import com.fasterxml.jackson.core.JsonGenerator; 11import com.fasterxml.jackson.core.JsonParser; 12import com.fasterxml.jackson.databind.DeserializationContext; 13import com.fasterxml.jackson.databind.JsonDeserializer; 14import com.fasterxml.jackson.databind.JsonSerializer; 15import com.fasterxml.jackson.databind.SerializerProvider; 16import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 17import com.fasterxml.jackson.databind.annotation.JsonSerialize; 18 19import org.tm.archive.util.JsonUtils; 20 21import java.io.IOException; 22 23/** 24 * Encapsulates the key material used to encrypt attachments on disk. 25 * 26 * There are two logical pieces of material, a deprecated set of keys used to encrypt 27 * legacy attachments, and a key that is used to encrypt attachments going forward. 28 */ 29public class AttachmentSecret { 30 31 @JsonProperty 32 @JsonSerialize(using = ByteArraySerializer.class) 33 @JsonDeserialize(using = ByteArrayDeserializer.class) 34 private byte[] classicCipherKey; 35 36 @JsonProperty 37 @JsonSerialize(using = ByteArraySerializer.class) 38 @JsonDeserialize(using = ByteArrayDeserializer.class) 39 private byte[] classicMacKey; 40 41 @JsonProperty 42 @JsonSerialize(using = ByteArraySerializer.class) 43 @JsonDeserialize(using = ByteArrayDeserializer.class) 44 private byte[] modernKey; 45 46 public AttachmentSecret(byte[] classicCipherKey, byte[] classicMacKey, byte[] modernKey) 47 { 48 this.classicCipherKey = classicCipherKey; 49 this.classicMacKey = classicMacKey; 50 this.modernKey = modernKey; 51 } 52 53 @SuppressWarnings("unused") 54 public AttachmentSecret() { 55 56 } 57 58 @JsonIgnore 59 byte[] getClassicCipherKey() { 60 return classicCipherKey; 61 } 62 63 @JsonIgnore 64 byte[] getClassicMacKey() { 65 return classicMacKey; 66 } 67 68 @JsonIgnore 69 public byte[] getModernKey() { 70 return modernKey; 71 } 72 73 @JsonIgnore 74 void setClassicCipherKey(byte[] classicCipherKey) { 75 this.classicCipherKey = classicCipherKey; 76 } 77 78 @JsonIgnore 79 void setClassicMacKey(byte[] classicMacKey) { 80 this.classicMacKey = classicMacKey; 81 } 82 83 public String serialize() { 84 try { 85 return JsonUtils.toJson(this); 86 } catch (IOException e) { 87 throw new AssertionError(e); 88 } 89 } 90 91 static AttachmentSecret fromString(@NonNull String value) { 92 try { 93 return JsonUtils.fromJson(value, AttachmentSecret.class); 94 } catch (IOException e) { 95 throw new AssertionError(e); 96 } 97 } 98 99 private static class ByteArraySerializer extends JsonSerializer<byte[]> { 100 @Override 101 public void serialize(byte[] value, JsonGenerator gen, SerializerProvider serializers) throws IOException { 102 gen.writeString(Base64.encodeToString(value, Base64.NO_WRAP | Base64.NO_PADDING)); 103 } 104 } 105 106 private static class ByteArrayDeserializer extends JsonDeserializer<byte[]> { 107 108 @Override 109 public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { 110 return Base64.decode(p.getValueAsString(), Base64.NO_WRAP | Base64.NO_PADDING); 111 } 112 } 113 114 115 116}