code that works
This commit is contained in:
parent
8de3930a00
commit
746899a2c3
12 changed files with 812 additions and 0 deletions
98
pkg/encrypt/encrypt.go
Normal file
98
pkg/encrypt/encrypt.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
package encrypt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"filippo.io/age"
|
||||
)
|
||||
|
||||
// Encrypt encrypts bytes into filename
|
||||
func Encrypt(data []byte, filename string, identity *age.X25519Identity) error {
|
||||
var (
|
||||
w io.WriteCloser
|
||||
out = &bytes.Buffer{}
|
||||
err error
|
||||
)
|
||||
|
||||
if identity == nil {
|
||||
return errors.New("nil identity??")
|
||||
}
|
||||
|
||||
if w, err = age.Encrypt(out, identity.Recipient()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
io.WriteString(w, string(data))
|
||||
if err = w.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
os.Truncate(filename, 0) // in case it exists already
|
||||
if err = os.WriteFile(filename, out.Bytes(), fs.FileMode(0600)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decrypt decrypts bytes from filename
|
||||
func Decrypt(filename string, identity *age.X25519Identity) ([]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, identity); 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
|
||||
}
|
101
pkg/encrypt/encrypt_test.go
Normal file
101
pkg/encrypt/encrypt_test.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
package encrypt
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"git.burning.moe/celediel/agedit/pkg/tmpfile"
|
||||
)
|
||||
|
||||
var generator = tmpfile.NewGenerator("test_", ".txt", 18)
|
||||
|
||||
// TestEncryptionDecryption writes a string to a file, encrypts it, then decrypts it, and reads the string.
|
||||
func TestEncryptionDecryption(t *testing.T) {
|
||||
var (
|
||||
strings_to_write = []string{
|
||||
"hello world",
|
||||
"hola mundo",
|
||||
"مرحبا بالعالم",
|
||||
"こんにちは世界",
|
||||
"你好世界",
|
||||
"Γειά σου Κόσμε",
|
||||
"Привіт Світ",
|
||||
"Բարեւ աշխարհ",
|
||||
"გამარჯობა მსოფლიო",
|
||||
"अभिवादन पृथ्वी",
|
||||
}
|
||||
)
|
||||
|
||||
id, err := NewIdentity()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, str := range strings_to_write {
|
||||
var (
|
||||
outname string = generator.GenerateFullPath()
|
||||
encrypted_outname string = outname + ".age"
|
||||
b []byte
|
||||
err error
|
||||
)
|
||||
|
||||
t.Run("testing writing "+str, func(t *testing.T) {
|
||||
if err = os.WriteFile(outname, []byte(str), fs.FileMode(0600)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if b, err = os.ReadFile(outname); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = Encrypt(b, encrypted_outname, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if b, err = Decrypt(encrypted_outname, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if string(b) != str {
|
||||
t.Fatal(string(b) + " isn't the same as " + str)
|
||||
}
|
||||
|
||||
if err = os.Remove(outname); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = os.Remove(encrypted_outname); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewIdentity creats a new identity, writes it to file, then re-reads it back from the file.
|
||||
func TestNewIdentity(t *testing.T) {
|
||||
for range 1000 {
|
||||
outfile := generator.GenerateFullPath()
|
||||
|
||||
identity, err := NewIdentity()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = WriteIdentityToFile(identity, outfile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
other_identity, err := 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())
|
||||
}
|
||||
os.Remove(outfile)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue