commit 661e92be08f6ebdc8bae760309bc2591b88911fe
parent a5e2bffec38ecafbe8165b016ac63ec412ed9f74
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 16 Jul 2022 14:40:22 -0400
helpers for file operations
Diffstat:
4 files changed, 22 insertions(+), 16 deletions(-)
diff --git a/cmd/lb-gitdiff/main.go b/cmd/lb-gitdiff/main.go
@@ -10,11 +10,7 @@ import (
func main() {
args := os.Args
- l, err := encrypt.NewLockbox(encrypt.LockboxOptions{File: args[len(args)-1]})
- if err != nil {
- misc.Die("unable to make lockbox model instance", err)
- }
- result, err := l.Decrypt()
+ result, err := encrypt.FromFile(args[len(args)-1])
if err != nil {
misc.Die("unable to read file", err)
}
diff --git a/cmd/lb-totp/main.go b/cmd/lb-totp/main.go
@@ -82,11 +82,7 @@ func display(token string, args cli.Arguments) error {
if !misc.PathExists(pathing) {
return errors.New("object does not exist")
}
- l, err := encrypt.NewLockbox(encrypt.LockboxOptions{File: pathing})
- if err != nil {
- return err
- }
- val, err := l.Decrypt()
+ val, err := encrypt.FromFile(pathing)
if err != nil {
return err
}
diff --git a/cmd/lb/main.go b/cmd/lb/main.go
@@ -125,12 +125,8 @@ func main() {
if password == "" {
misc.Die("empty password provided", errors.New("password can NOT be empty"))
}
- l, err := encrypt.NewLockbox(encrypt.LockboxOptions{File: entry})
- if err != nil {
- misc.Die("unable to make lockbox model instance", err)
- }
- if err := l.Encrypt([]byte(password)); err != nil {
- misc.Die("failed to save password", err)
+ if err := encrypt.ToFile(entry, []byte(password)); err != nil {
+ misc.Die("unable to encrypt object", err)
}
fmt.Println("")
hooks.Run(hooks.Insert, hooks.PostStep)
diff --git a/internal/encrypt/core.go b/internal/encrypt/core.go
@@ -41,6 +41,24 @@ type (
}
)
+// FromFile decrypts a file-system based encrypted file.
+func FromFile(file string) ([]byte, error) {
+ l, err := NewLockbox(LockboxOptions{File: file})
+ if err != nil {
+ return nil, err
+ }
+ return l.Decrypt()
+}
+
+// ToFile encrypts data to a file-system based file.
+func ToFile(file string, data []byte) error {
+ l, err := NewLockbox(LockboxOptions{File: file})
+ if err != nil {
+ return err
+ }
+ return l.Encrypt(data)
+}
+
// NewLockbox creates a new usable lockbox instance.
func NewLockbox(options LockboxOptions) (Lockbox, error) {
return newLockbox(options.Key, options.KeyMode, options.File)