Skip to content

Commit da21917

Browse files
committed
age: add ExampleDecryptReaderAt with zip.NewReader
1 parent 2ff5d34 commit da21917

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

age.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ func Decrypt(src io.Reader, identities ...Identity) (io.Reader, error) {
284284
// DecryptReaderAt takes an underlying [io.ReaderAt] and its total encrypted
285285
// size, and returns a ReaderAt of the decrypted plaintext and the plaintext
286286
// size. These can be used for example to instantiate an [io.SectionReader],
287-
// which implements [io.Reader] and [io.Seeker]. Note that ReaderAt by
288-
// definition disregards the seek position of src.
287+
// which implements [io.Reader] and [io.Seeker], or for [zip.NewReader].
288+
// Note that ReaderAt by definition disregards the seek position of src.
289289
//
290290
// The ReadAt method of the returned ReaderAt can be called concurrently.
291291
// The ReaderAt will internally cache the most recently decrypted chunk.

age_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
package age_test
66

77
import (
8+
"archive/zip"
89
"bytes"
910
"errors"
1011
"fmt"
1112
"io"
13+
"io/fs"
1214
"log"
1315
"os"
1416
"slices"
@@ -191,6 +193,40 @@ func TestEncryptDecryptScrypt(t *testing.T) {
191193
}
192194
}
193195

196+
func ExampleDecryptReaderAt() {
197+
identity, err := age.ParseX25519Identity(privateKey)
198+
if err != nil {
199+
log.Fatalf("Failed to parse private key: %v", err)
200+
}
201+
202+
f, err := os.Open("testdata/example.zip.age")
203+
if err != nil {
204+
log.Fatalf("Failed to open file: %v", err)
205+
}
206+
stat, err := f.Stat()
207+
if err != nil {
208+
log.Fatalf("Failed to stat file: %v", err)
209+
}
210+
211+
r, size, err := age.DecryptReaderAt(f, stat.Size(), identity)
212+
if err != nil {
213+
log.Fatalf("Failed to open encrypted file: %v", err)
214+
}
215+
216+
z, err := zip.NewReader(r, size)
217+
if err != nil {
218+
log.Fatalf("Failed to open zip: %v", err)
219+
}
220+
contents, err := fs.ReadFile(z, "example.txt")
221+
if err != nil {
222+
log.Fatalf("Failed to read file from zip: %v", err)
223+
}
224+
225+
fmt.Printf("File contents: %q\n", contents)
226+
// Output:
227+
// File contents: "Black lives matter."
228+
}
229+
194230
func TestParseIdentities(t *testing.T) {
195231
tests := []struct {
196232
name string

testdata/example.zip.age

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
age-encryption.org/v1
2+
-> X25519 5CD81lZA72aQi0v6EnniOGkwaswpZ0AxCZNdiUVzP04
3+
ol9DvdkiZWeRI4vMKRBVNxowDKwir4UPqYinSM5zqUI
4+
--- 2tyNGCaPoT6UnuOy7sQJf1eXn4pb7z2ukSgTDIxrJxU
5+
W
6+
dtUb�T���0���(�yKAP�dr1�M~� ���kX>�����c܍[�$�9�,G�{턚F����tk��*}�9�T���ޱ�hLЏ��W�-Rd���ˣ���H�SdU��������ĚFsÈ��2y+)��]/��,k=�8(X���R�A��01R��Y������� �k4N��6��t�v���b���c � �_F0d�v�x�����$X�~ /

0 commit comments

Comments
 (0)