Compare commits
No commits in common. "c4ee8e7b046791ebc88ad4b1d6d9ed20ba80f35b" and "52a87a41da88ce2caf9d849e43f0b69848936091" have entirely different histories.
c4ee8e7b04
...
52a87a41da
|
@ -9,7 +9,6 @@ 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"
|
||||
|
@ -22,7 +21,7 @@ import (
|
|||
const (
|
||||
name string = "agedit"
|
||||
usage string = "Edit age encrypted files with your $EDITOR"
|
||||
version string = "0.2.0"
|
||||
version string = "0.1.1"
|
||||
help_template string = `NAME:
|
||||
{{.Name}} {{if .Version}}v{{.Version}}{{end}} - {{.Usage}}
|
||||
|
||||
|
@ -243,13 +242,19 @@ func action(ctx *cli.Context) error {
|
|||
|
||||
// get recipients from specified identities
|
||||
for _, id := range identities {
|
||||
if actual_id, ok := id.(*age.X25519Identity); ok {
|
||||
recipients = append(recipients, actual_id.Recipient())
|
||||
// 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)
|
||||
}
|
||||
|
||||
recipients = append(recipients, actual_id.Recipient())
|
||||
}
|
||||
|
||||
// try to decrypt the file
|
||||
decrypted, err := decrypt.Decrypt(input_file, identities...)
|
||||
decrypted, err := encrypt.Decrypt(input_file, identities...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -38,3 +38,61 @@ 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
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package encrypt_decrypt_test
|
||||
package encrypt
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
|
@ -6,9 +6,6 @@ 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"
|
||||
)
|
||||
|
||||
|
@ -52,11 +49,11 @@ func TestEncryptionDecryption(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
|
||||
if b, err = decrypt.Decrypt(encrypted_outname, id); err != nil {
|
||||
if b, err = Decrypt(encrypted_outname, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
@ -108,13 +105,13 @@ func TestMultipleIdentities(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
|
||||
// try decrypting with each identity
|
||||
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)
|
||||
}
|
||||
if string(b) != str {
|
||||
|
@ -123,7 +120,7 @@ func TestMultipleIdentities(t *testing.T) {
|
|||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
@ -148,23 +145,23 @@ func TestNewIdentity(t *testing.T) {
|
|||
for range 1000 {
|
||||
outfile := generator.GenerateFullPath()
|
||||
|
||||
id, err := identity.NewIdentity()
|
||||
identity, err := NewIdentity()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = identity.WriteIdentityToFile(id, outfile)
|
||||
err = WriteIdentityToFile(identity, outfile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
other_identity, err := identity.ReadIdentityFromFile(outfile)
|
||||
other_identity, err := ReadIdentityFromFile(outfile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if id.Recipient().String() != other_identity.Recipient().String() && id.String() != other_identity.String() {
|
||||
t.Fatal("Identities don't match!", id.Recipient(), "!=", id.Recipient())
|
||||
if identity.Recipient().String() != other_identity.Recipient().String() && identity.String() != other_identity.String() {
|
||||
t.Fatal("Identities don't match!", identity.Recipient(), "!=", identity.Recipient())
|
||||
}
|
||||
os.Remove(outfile)
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue