-
Hey all, while I cannot rule out I just made a stupid mistake - this is basically my first go "project" - i cannot get my head around why the created reader is not able to read the decrypted data in the following scenario (links to code and the relevant test case):
While the note text can be decrypted, during the decryption of the attachment file, the following error is caught in the test:
The function in question is a wrapper around the decryption function which can be found here. The first call to age.Decrypt results in a successful decyption of the note's text. Afterwards a loop iterates over the file attachments of a note and the second call results in the mentioned error. I debugged the code multiple times, but still fail to understand why this happens, so I hope to get some hints what to look for here. The test routine for reference: func TestAttachmentEncryption(t *testing.T) {
note := model.NewNote("Title", "Note")
attachment := model.NewAttachment("attachment.txt", []byte("This is some data."))
note.Attachments = append(note.Attachments, attachment)
i1, err := age.ParseX25519Identity(key)
if err != nil {
t.Fatalf("Could not parse identity: %v", err)
}
r1 := i1.Recipient()
enc, err := note.ToEncryptedNote(*r1)
if err != nil {
t.Fatal(err)
}
j, err := enc.Json()
if err != nil {
t.Fatal(err)
}
t.Logf(">>> DEBUG: Encrypted note: %s", string(j))
dec, err := enc.ToDecryptedNote(i1) // <- error is "generated" here; the function calls
if err != nil {
t.Fatal(err)
}
j2, err := json.Marshal(dec)
if err != nil {
t.Fatal(err)
}
t.Logf(">>> DEBUG: Decrypted note: %s", string(j2))
if len(dec.Attachments[0].Content) != len(attachment.Content) {
t.Fatal("Content length mismatch")
}
for i := range dec.Attachments[0].Content {
if dec.Attachments[0].Content[i] != attachment.Content[i] {
t.Fatalf("Mismatch of content at pos %d.", i)
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
In your attachment encryption loop, you call These deferred calls run at the end of the function, not at the end of each loop iteration, so the writer is not being properly closed before you append the ciphertext to the encryptedAttachments list. |
Beta Was this translation helpful? Give feedback.
In your attachment encryption loop, you call
defer w.Close()
.https://github.com/3c7/age-encrypted-notebook/blob/1e57ce9cd739e1b2ed6604aff48ba32732ef5de3/internal/model/model.go#L194
These deferred calls run at the end of the function, not at the end of each loop iteration, so the writer is not being properly closed before you append the ciphertext to the encryptedAttachments list.