Compare commits

..

No commits in common. "c4ee8e7b046791ebc88ad4b1d6d9ed20ba80f35b" and "52a87a41da88ce2caf9d849e43f0b69848936091" have entirely different histories.

5 changed files with 79 additions and 94 deletions

View file

@ -9,7 +9,6 @@ import (
"filippo.io/age" "filippo.io/age"
"git.burning.moe/celediel/agedit/internal/config" "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/editor"
"git.burning.moe/celediel/agedit/pkg/encrypt" "git.burning.moe/celediel/agedit/pkg/encrypt"
"git.burning.moe/celediel/agedit/pkg/env" "git.burning.moe/celediel/agedit/pkg/env"
@ -22,7 +21,7 @@ import (
const ( const (
name string = "agedit" name string = "agedit"
usage string = "Edit age encrypted files with your $EDITOR" usage string = "Edit age encrypted files with your $EDITOR"
version string = "0.2.0" version string = "0.1.1"
help_template string = `NAME: help_template string = `NAME:
{{.Name}} {{if .Version}}v{{.Version}}{{end}} - {{.Usage}} {{.Name}} {{if .Version}}v{{.Version}}{{end}} - {{.Usage}}
@ -243,13 +242,19 @@ func action(ctx *cli.Context) error {
// get recipients from specified identities // get recipients from specified identities
for _, id := range identities { for _, id := range identities {
if actual_id, ok := id.(*age.X25519Identity); ok { // TODO: figure out how age actually intends for
recipients = append(recipients, actual_id.Recipient()) // 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)
} }
recipients = append(recipients, actual_id.Recipient())
} }
// try to decrypt the file // try to decrypt the file
decrypted, err := decrypt.Decrypt(input_file, identities...) decrypted, err := encrypt.Decrypt(input_file, identities...)
if err != nil { if err != nil {
return err return err
} }

View file

@ -1,32 +0,0 @@
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,3 +38,61 @@ func Encrypt(data []byte, filename string, recipients ...age.Recipient) error {
return nil 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_decrypt_test package encrypt
import ( import (
"io/fs" "io/fs"
@ -6,9 +6,6 @@ import (
"testing" "testing"
"filippo.io/age" "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" "git.burning.moe/celediel/agedit/pkg/tmpfile"
) )
@ -52,11 +49,11 @@ func TestEncryptionDecryption(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if err = encrypt.Encrypt(b, encrypted_outname, id.Recipient()); err != nil { if err = Encrypt(b, encrypted_outname, id.Recipient()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if b, err = decrypt.Decrypt(encrypted_outname, id); err != nil { if b, err = Decrypt(encrypted_outname, id); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -108,13 +105,13 @@ func TestMultipleIdentities(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if err = encrypt.Encrypt(b, encrypted_outname, recipients...); err != nil { if err = Encrypt(b, encrypted_outname, recipients...); err != nil {
t.Fatal(err) t.Fatal(err)
} }
// try decrypting with each identity // try decrypting with each identity
for _, id := range identities { for _, id := range identities {
if b, err = decrypt.Decrypt(encrypted_outname, id); err != nil { if b, err = Decrypt(encrypted_outname, id); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if string(b) != str { if string(b) != str {
@ -123,7 +120,7 @@ func TestMultipleIdentities(t *testing.T) {
} }
// then all of them because why not // then all of them because why not
if b, err = decrypt.Decrypt(encrypted_outname, identities...); err != nil { if b, err = Decrypt(encrypted_outname, identities...); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -148,23 +145,23 @@ func TestNewIdentity(t *testing.T) {
for range 1000 { for range 1000 {
outfile := generator.GenerateFullPath() outfile := generator.GenerateFullPath()
id, err := identity.NewIdentity() identity, err := NewIdentity()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = identity.WriteIdentityToFile(id, outfile) err = WriteIdentityToFile(identity, outfile)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
other_identity, err := identity.ReadIdentityFromFile(outfile) other_identity, err := ReadIdentityFromFile(outfile)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if id.Recipient().String() != other_identity.Recipient().String() && id.String() != other_identity.String() { if identity.Recipient().String() != other_identity.Recipient().String() && identity.String() != other_identity.String() {
t.Fatal("Identities don't match!", id.Recipient(), "!=", id.Recipient()) t.Fatal("Identities don't match!", identity.Recipient(), "!=", identity.Recipient())
} }
os.Remove(outfile) os.Remove(outfile)
} }

View file

@ -1,43 +0,0 @@
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
}