From 29b44aca7842b459815c50426fddbdda3efa44a3 Mon Sep 17 00:00:00 2001 From: nomionz Date: Sun, 28 Jan 2024 14:19:24 +0100 Subject: [PATCH] refactor file encryption and decryption functions --- pkg/fs/fs.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/pkg/fs/fs.go b/pkg/fs/fs.go index 87122f0..b35ca1e 100644 --- a/pkg/fs/fs.go +++ b/pkg/fs/fs.go @@ -5,34 +5,28 @@ import ( "os" ) -func Decrypt(path string, pass []byte) error { - file, err := os.ReadFile(path) - - if err != nil { - return err - } - - dec, err := cipher.Decrypt(file, pass) +type cipherFunc func(data, pass []byte) ([]byte, error) - if err != nil { - return err - } - - return os.WriteFile(path, dec, 0644) +func Decrypt(path string, pass []byte) error { + return processFile(path, pass, cipher.Decrypt) } func Encrypt(path string, pass []byte) error { + return processFile(path, pass, cipher.Encrypt) +} + +func processFile(path string, pass []byte, process cipherFunc) error { file, err := os.ReadFile(path) if err != nil { return err } - enc, err := cipher.Encrypt(file, pass) + result, err := process(file, pass) if err != nil { return err } - return os.WriteFile(path, enc, 0644) + return os.WriteFile(path, result, 0644) }