age: add ExampleDecryptReaderAt with zip.NewReader

This commit is contained in:
Filippo Valsorda
2025-12-26 21:35:42 +01:00
parent 2ff5d341f6
commit da2191789a
3 changed files with 44 additions and 2 deletions

View File

@@ -5,10 +5,12 @@
package age_test
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"slices"
@@ -191,6 +193,40 @@ func TestEncryptDecryptScrypt(t *testing.T) {
}
}
func ExampleDecryptReaderAt() {
identity, err := age.ParseX25519Identity(privateKey)
if err != nil {
log.Fatalf("Failed to parse private key: %v", err)
}
f, err := os.Open("testdata/example.zip.age")
if err != nil {
log.Fatalf("Failed to open file: %v", err)
}
stat, err := f.Stat()
if err != nil {
log.Fatalf("Failed to stat file: %v", err)
}
r, size, err := age.DecryptReaderAt(f, stat.Size(), identity)
if err != nil {
log.Fatalf("Failed to open encrypted file: %v", err)
}
z, err := zip.NewReader(r, size)
if err != nil {
log.Fatalf("Failed to open zip: %v", err)
}
contents, err := fs.ReadFile(z, "example.txt")
if err != nil {
log.Fatalf("Failed to read file from zip: %v", err)
}
fmt.Printf("File contents: %q\n", contents)
// Output:
// File contents: "Black lives matter."
}
func TestParseIdentities(t *testing.T) {
tests := []struct {
name string