diff --git a/internal/crypto/encryption.go b/internal/crypto/encryption.go index 1d7b3cb..d3380a7 100644 --- a/internal/crypto/encryption.go +++ b/internal/crypto/encryption.go @@ -2,6 +2,7 @@ package crypto import ( "bytes" + "errors" "fmt" "io" "sync" @@ -25,7 +26,7 @@ type Encryptor struct { // public keys are invalid or if no recipients are specified. func NewEncryptor(publicKeys []string) (*Encryptor, error) { if len(publicKeys) == 0 { - return nil, fmt.Errorf("at least one recipient is required") + return nil, errors.New("at least one recipient is required") } recipients := make([]age.Recipient, 0, len(publicKeys)) @@ -34,6 +35,7 @@ func NewEncryptor(publicKeys []string) (*Encryptor, error) { if err != nil { return nil, fmt.Errorf("parsing age recipient %s: %w", key, err) } + recipients = append(recipients, recipient) } @@ -126,7 +128,7 @@ func (e *Encryptor) EncryptWriter(dst io.Writer) (io.WriteCloser, error) { // of the public keys are invalid or if no recipients are specified. func (e *Encryptor) UpdateRecipients(publicKeys []string) error { if len(publicKeys) == 0 { - return fmt.Errorf("at least one recipient is required") + return errors.New("at least one recipient is required") } recipients := make([]age.Recipient, 0, len(publicKeys)) @@ -135,6 +137,7 @@ func (e *Encryptor) UpdateRecipients(publicKeys []string) error { if err != nil { return fmt.Errorf("parsing age recipient %s: %w", key, err) } + recipients = append(recipients, recipient) } diff --git a/internal/crypto/encryption_test.go b/internal/crypto/encryption_test.go index ddd92ca..584b216 100644 --- a/internal/crypto/encryption_test.go +++ b/internal/crypto/encryption_test.go @@ -58,10 +58,12 @@ func TestEncryptorMultipleRecipients(t *testing.T) { if err != nil { t.Fatalf("failed to generate identity1: %v", err) } + identity2, err := age.GenerateX25519Identity() if err != nil { t.Fatalf("failed to generate identity2: %v", err) } + identity3, err := age.GenerateX25519Identity() if err != nil { t.Fatalf("failed to generate identity3: %v", err) @@ -123,6 +125,7 @@ func TestEncryptorUpdateRecipients(t *testing.T) { // Encrypt with first key plaintext := []byte("test data") + ciphertext1, err := enc.Encrypt(plaintext) if err != nil { t.Fatalf("failed to encrypt: %v", err) @@ -143,6 +146,7 @@ func TestEncryptorUpdateRecipients(t *testing.T) { if _, err := age.Decrypt(bytes.NewReader(ciphertext1), identity1); err != nil { t.Error("failed to decrypt with identity1") } + if _, err := age.Decrypt(bytes.NewReader(ciphertext1), identity2); err == nil { t.Error("should not decrypt with identity2") } @@ -151,6 +155,7 @@ func TestEncryptorUpdateRecipients(t *testing.T) { if _, err := age.Decrypt(bytes.NewReader(ciphertext2), identity2); err != nil { t.Error("failed to decrypt with identity2") } + if _, err := age.Decrypt(bytes.NewReader(ciphertext2), identity1); err == nil { t.Error("should not decrypt with identity1") }