Implement a bit of Encryption
This commit is contained in:
parent
c253ca6fdf
commit
65d8a457a3
@ -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
|
||||
|
@ -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:]
|
||||
|
@ -3,7 +3,9 @@ Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
||||
*/
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user