Skip to content

Commit

Permalink
refactor file encryption and decryption functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nomionz committed Jan 28, 2024
1 parent 50b90ed commit 29b44ac
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions pkg/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 29b44ac

Please sign in to comment.