@@ -41,26 +41,42 @@ data class RemoteAttachment(
41
41
throw XMTPException (" no remote attachment payload" )
42
42
}
43
43
44
- if (Hash .sha256(payload).toHex() != contentDigest) {
45
- throw XMTPException (" contentDigest does not match" )
46
- }
47
-
48
- val aes = Ciphertext .Aes256gcmHkdfsha256 .newBuilder().also {
49
- it.hkdfSalt = salt
50
- it.gcmNonce = nonce
51
- it.payload = payload.toByteString()
52
- }.build()
53
-
54
- val ciphertext = Ciphertext .newBuilder().also {
55
- it.aes256GcmHkdfSha256 = aes
56
- }.build()
44
+ val encrypted = EncryptedEncodedContent (
45
+ contentDigest,
46
+ secret,
47
+ salt,
48
+ nonce,
49
+ payload.toByteString(),
50
+ contentLength,
51
+ filename,
52
+ )
57
53
58
- val decrypted = Crypto .decrypt(secret = secret.toByteArray(), ciphertext = ciphertext )
54
+ val decrypted = decryptEncoded(encrypted )
59
55
60
- return EncodedContent .parseFrom( decrypted) .decoded<T >()
56
+ return decrypted.decoded<T >()
61
57
}
62
58
63
59
companion object {
60
+ fun decryptEncoded (encrypted : EncryptedEncodedContent ): EncodedContent {
61
+ if (Hash .sha256(encrypted.payload.toByteArray()).toHex() != encrypted.contentDigest) {
62
+ throw XMTPException (" contentDigest does not match" )
63
+ }
64
+
65
+ val aes = Ciphertext .Aes256gcmHkdfsha256 .newBuilder().also {
66
+ it.hkdfSalt = encrypted.salt
67
+ it.gcmNonce = encrypted.nonce
68
+ it.payload = encrypted.payload
69
+ }.build()
70
+
71
+ val ciphertext = Ciphertext .newBuilder().also {
72
+ it.aes256GcmHkdfSha256 = aes
73
+ }.build()
74
+
75
+ val decrypted = Crypto .decrypt(encrypted.secret.toByteArray(), ciphertext)
76
+
77
+ return EncodedContent .parseFrom(decrypted)
78
+ }
79
+
64
80
fun <T > encodeEncrypted (content : T , codec : ContentCodec <T >): EncryptedEncodedContent {
65
81
val secret = SecureRandom ().generateSeed(32 )
66
82
val encodedContent = codec.encode(content).toByteArray()
0 commit comments