Compare commits

...

3 commits

Author SHA1 Message Date
Lilian Jónsdóttir c4ee8e7b04 version bump - v0.2.0
- move Decrypt and Identity related functions to their own packages
- hide default text for `force` cli flag
- get recipients the (probably) right way
- update to go 1.22
2024-04-01 13:29:14 -07:00
Lilian Jónsdóttir 633673200a move decryption and identity stuff to their own packages 2024-04-01 13:28:01 -07:00
Lilian Jónsdóttir 67ab59ec48 get recipient in a non-dumb way 2024-04-01 11:45:29 -07:00
5 changed files with 94 additions and 79 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"
@ -21,7 +22,7 @@ import (
const (
name string = "agedit"
usage string = "Edit age encrypted files with your $EDITOR"
version string = "0.1.1"
version string = "0.2.0"
help_template string = `NAME:
{{.Name}} {{if .Version}}v{{.Version}}{{end}} - {{.Usage}}
@ -242,19 +243,13 @@ func action(ctx *cli.Context) error {
// get recipients from specified identities
for _, id := range identities {
// TODO: figure out how age actually intends for
// TODO: a recpient to be retrieved from an age.Identity
// TODO: beccause this is stupid and I hate it
actual_id, err := age.ParseX25519Identity(fmt.Sprint(id))
if err != nil {
return fmt.Errorf("couldn't get recipient? %v", err)
if actual_id, ok := id.(*age.X25519Identity); ok {
recipients = append(recipients, actual_id.Recipient())
}
recipients = append(recipients, actual_id.Recipient())
}
// 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
}