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

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
}