From 65d8a457a3f677adf7554c3b29aa605f4e205b26 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 15 May 2024 15:10:38 -0500 Subject: [PATCH] Implement a bit of Encryption --- cmd/rofi.go | 5 ++++- util/crypt.go | 19 +++++++++++++------ util/files.go | 24 +++++++++++++++++++++--- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/cmd/rofi.go b/cmd/rofi.go index 8979f98..f525a6f 100644 --- a/cmd/rofi.go +++ b/cmd/rofi.go @@ -30,10 +30,12 @@ func runRofi(cmd *cobra.Command, args []string) error { for i := range args { path = append(path, strings.Split(args[i], "/")...) } + var db *models.KeePassDB var havePassword bool + pwFile := fmt.Sprintf("%s%s", ConfigDir, "pw") - pass, err := util.ReadFile(pwFile) + pass, err := util.ReadPWFile(pwFile) if err != nil { havePassword = false // See if we have a password... @@ -46,6 +48,7 @@ func runRofi(cmd *cobra.Command, args []string) error { } else { havePassword = true } + db, err = models.NewKeePassDB(viper.GetString("database"), pass) if err != nil { return err diff --git a/util/crypt.go b/util/crypt.go index f114a54..432c96b 100644 --- a/util/crypt.go +++ b/util/crypt.go @@ -6,26 +6,33 @@ package util import ( "crypto/aes" "crypto/cipher" + "crypto/rand" "encoding/base64" "encoding/hex" "errors" "fmt" "io" + "time" ) +func GenTodayKey() string { + key := []byte(time.Now().Format(fmt.Sprintf("%s%s%s ", time.DateOnly, time.DateOnly, time.DateOnly))) + return hex.EncodeToString(key) //convert to string for saving +} + func Encrypt(key, input string) (string, error) { - key, _ := hex.DecodeString(key) + btKey, _ := hex.DecodeString(key) plaintext := []byte(input) // Create a new Cipher Block from the key - block, err := aes.NewCipher(key) + block, err := aes.NewCipher(btKey) if err != nil { return "", err } ciphertext := make([]byte, aes.BlockSize+len(plaintext)) iv := ciphertext[:aes.BlockSize] - if _, err := io.ReadFull(rand.reader, iv); err != nil { + if _, err := io.ReadFull(rand.Reader, iv); err != nil { return "", err } @@ -37,16 +44,16 @@ func Encrypt(key, input string) (string, error) { } func Decrypt(key, input string) (string, error) { - key, _ := hex.DecodeString(key) + btKey, _ := hex.DecodeString(key) ciphertext, _ := base64.URLEncoding.DecodeString(input) - block, err := aes.NewCipher(key) + block, err := aes.NewCipher(btKey) if err != nil { return "", err } if len(ciphertext) < aes.BlockSize { - return errors.New("ciphertext is too short") + return "", errors.New("ciphertext is too short") } iv := ciphertext[:aes.BlockSize] ciphertext = ciphertext[aes.BlockSize:] diff --git a/util/files.go b/util/files.go index 8b5e9bf..5353a86 100644 --- a/util/files.go +++ b/util/files.go @@ -3,7 +3,9 @@ Copyright © 2024 Brian Buller */ package util -import "io/ioutil" +import ( + "io/ioutil" +) func ReadFile(path string) (string, error) { var bytesRead []byte @@ -14,6 +16,22 @@ func ReadFile(path string) (string, error) { return string(bytesRead), nil } -func WritePWFile(path, contents string) error { - return ioutil.WriteFile(path, []byte(contents), 0600) +func ReadPWFile(path string) (string, error) { + encPass, err := ReadFile(path) + if err != nil { + return "", err + } + var pass string + pass, err = Decrypt(GenTodayKey(), encPass) + if err != nil { + return "", err + } + return pass, nil +} +func WritePWFile(path, pw string) error { + pass, err := Encrypt(GenTodayKey(), pw) + if err != nil { + return err + } + return ioutil.WriteFile(path, []byte(pass), 0600) }