move decryption and identity stuff to their own packages

This commit is contained in:
Lilian Jónsdóttir 2024-04-01 13:28:01 -07:00
parent 67ab59ec48
commit 633673200a
5 changed files with 91 additions and 70 deletions

View file

@ -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
}

32
pkg/decrypt/decrypt.go Normal file
View file

@ -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
}

View file

@ -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
}

View file

@ -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)
}

43
pkg/identity/identity.go Normal file
View file

@ -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
}