From 633673200a02d2bc4c1973129e2cb626b930d590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Mon, 1 Apr 2024 13:28:01 -0700 Subject: [PATCH] move decryption and identity stuff to their own packages --- cmd/agedit/cli.go | 3 +- pkg/decrypt/decrypt.go | 32 ++++++++++ pkg/encrypt/encrypt.go | 58 ------------------- ...ncrypt_test.go => encrypt_decrypt_test.go} | 25 ++++---- pkg/identity/identity.go | 43 ++++++++++++++ 5 files changed, 91 insertions(+), 70 deletions(-) create mode 100644 pkg/decrypt/decrypt.go rename pkg/{encrypt/encrypt_test.go => encrypt_decrypt_test.go} (77%) create mode 100644 pkg/identity/identity.go diff --git a/cmd/agedit/cli.go b/cmd/agedit/cli.go index c563c8c..f976847 100644 --- a/cmd/agedit/cli.go +++ b/cmd/agedit/cli.go @@ -9,6 +9,7 @@ import ( "filippo.io/age" "git.burning.moe/celediel/agedit/internal/config" + "git.burning.moe/celediel/agedit/pkg/decrypt" "git.burning.moe/celediel/agedit/pkg/editor" "git.burning.moe/celediel/agedit/pkg/encrypt" "git.burning.moe/celediel/agedit/pkg/env" @@ -248,7 +249,7 @@ func action(ctx *cli.Context) error { } // try to decrypt the file - decrypted, err := encrypt.Decrypt(input_file, identities...) + decrypted, err := decrypt.Decrypt(input_file, identities...) if err != nil { return err } diff --git a/pkg/decrypt/decrypt.go b/pkg/decrypt/decrypt.go new file mode 100644 index 0000000..a73ec9e --- /dev/null +++ b/pkg/decrypt/decrypt.go @@ -0,0 +1,32 @@ +package decrypt + +import ( + "bytes" + "io" + "os" + + "filippo.io/age" +) + +// Decrypt decrypts bytes from filename +func Decrypt(filename string, identities ...age.Identity) ([]byte, error) { + var ( + f *os.File + r io.Reader + err error + out = &bytes.Buffer{} + ) + if f, err = os.Open(filename); err != nil { + return nil, err + } + + if r, err = age.Decrypt(f, identities...); err != nil { + return nil, err + } + + if _, err := io.Copy(out, r); err != nil { + return nil, err + } + + return out.Bytes(), nil +} diff --git a/pkg/encrypt/encrypt.go b/pkg/encrypt/encrypt.go index 152fedb..f5a337c 100644 --- a/pkg/encrypt/encrypt.go +++ b/pkg/encrypt/encrypt.go @@ -38,61 +38,3 @@ func Encrypt(data []byte, filename string, recipients ...age.Recipient) error { return nil } - -// Decrypt decrypts bytes from filename -func Decrypt(filename string, identities ...age.Identity) ([]byte, error) { - var ( - f *os.File - r io.Reader - err error - out = &bytes.Buffer{} - ) - if f, err = os.Open(filename); err != nil { - return nil, err - } - - if r, err = age.Decrypt(f, identities...); err != nil { - return nil, err - } - - if _, err := io.Copy(out, r); err != nil { - return nil, err - } - - return out.Bytes(), nil -} - -// NewIdentity generates a new Age identity -func NewIdentity() (*age.X25519Identity, error) { - id, err := age.GenerateX25519Identity() - if err != nil { - return nil, err - } - - return id, nil -} - -// ReadIdentityFromFile reads the identity from the supplied filename -func ReadIdentityFromFile(filename string) (*age.X25519Identity, error) { - bytes, err := os.ReadFile(filename) - if err != nil { - return nil, err - } - - id, err := age.ParseX25519Identity(string(bytes)) - if err != nil { - return nil, err - } - - return id, nil -} - -// WriteIdentityToFile writes the supplied identity to the supplied filename -func WriteIdentityToFile(id *age.X25519Identity, filename string) error { - err := os.WriteFile(filename, []byte(id.String()), fs.FileMode(0600)) - if err != nil { - return err - } - - return nil -} diff --git a/pkg/encrypt/encrypt_test.go b/pkg/encrypt_decrypt_test.go similarity index 77% rename from pkg/encrypt/encrypt_test.go rename to pkg/encrypt_decrypt_test.go index ffaa034..a680531 100644 --- a/pkg/encrypt/encrypt_test.go +++ b/pkg/encrypt_decrypt_test.go @@ -1,4 +1,4 @@ -package encrypt +package encrypt_decrypt_test import ( "io/fs" @@ -6,6 +6,9 @@ import ( "testing" "filippo.io/age" + "git.burning.moe/celediel/agedit/pkg/decrypt" + "git.burning.moe/celediel/agedit/pkg/encrypt" + "git.burning.moe/celediel/agedit/pkg/identity" "git.burning.moe/celediel/agedit/pkg/tmpfile" ) @@ -49,11 +52,11 @@ func TestEncryptionDecryption(t *testing.T) { t.Fatal(err) } - if err = Encrypt(b, encrypted_outname, id.Recipient()); err != nil { + if err = encrypt.Encrypt(b, encrypted_outname, id.Recipient()); err != nil { t.Fatal(err) } - if b, err = Decrypt(encrypted_outname, id); err != nil { + if b, err = decrypt.Decrypt(encrypted_outname, id); err != nil { t.Fatal(err) } @@ -105,13 +108,13 @@ func TestMultipleIdentities(t *testing.T) { t.Fatal(err) } - if err = Encrypt(b, encrypted_outname, recipients...); err != nil { + if err = encrypt.Encrypt(b, encrypted_outname, recipients...); err != nil { t.Fatal(err) } // try decrypting with each identity for _, id := range identities { - if b, err = Decrypt(encrypted_outname, id); err != nil { + if b, err = decrypt.Decrypt(encrypted_outname, id); err != nil { t.Fatal(err) } if string(b) != str { @@ -120,7 +123,7 @@ func TestMultipleIdentities(t *testing.T) { } // then all of them because why not - if b, err = Decrypt(encrypted_outname, identities...); err != nil { + if b, err = decrypt.Decrypt(encrypted_outname, identities...); err != nil { t.Fatal(err) } @@ -145,23 +148,23 @@ func TestNewIdentity(t *testing.T) { for range 1000 { outfile := generator.GenerateFullPath() - identity, err := NewIdentity() + id, err := identity.NewIdentity() if err != nil { t.Fatal(err) } - err = WriteIdentityToFile(identity, outfile) + err = identity.WriteIdentityToFile(id, outfile) if err != nil { t.Fatal(err) } - other_identity, err := ReadIdentityFromFile(outfile) + other_identity, err := identity.ReadIdentityFromFile(outfile) if err != nil { t.Fatal(err) } - if identity.Recipient().String() != other_identity.Recipient().String() && identity.String() != other_identity.String() { - t.Fatal("Identities don't match!", identity.Recipient(), "!=", identity.Recipient()) + if id.Recipient().String() != other_identity.Recipient().String() && id.String() != other_identity.String() { + t.Fatal("Identities don't match!", id.Recipient(), "!=", id.Recipient()) } os.Remove(outfile) } diff --git a/pkg/identity/identity.go b/pkg/identity/identity.go new file mode 100644 index 0000000..58b8c24 --- /dev/null +++ b/pkg/identity/identity.go @@ -0,0 +1,43 @@ +package identity + +import ( + "io/fs" + "os" + + "filippo.io/age" +) + +// NewIdentity generates a new Age identity +func NewIdentity() (*age.X25519Identity, error) { + id, err := age.GenerateX25519Identity() + if err != nil { + return nil, err + } + + return id, nil +} + +// ReadIdentityFromFile reads the identity from the supplied filename +func ReadIdentityFromFile(filename string) (*age.X25519Identity, error) { + bytes, err := os.ReadFile(filename) + if err != nil { + return nil, err + } + + id, err := age.ParseX25519Identity(string(bytes)) + if err != nil { + return nil, err + } + + return id, nil +} + +// WriteIdentityToFile writes the supplied identity to the supplied filename +func WriteIdentityToFile(id *age.X25519Identity, filename string) error { + err := os.WriteFile(filename, []byte(id.String()), fs.FileMode(0600)) + if err != nil { + return err + } + + return nil +}