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 {
|
for i := range args {
|
||||||
path = append(path, strings.Split(args[i], "/")...)
|
path = append(path, strings.Split(args[i], "/")...)
|
||||||
}
|
}
|
||||||
|
|
||||||
var db *models.KeePassDB
|
var db *models.KeePassDB
|
||||||
var havePassword bool
|
var havePassword bool
|
||||||
|
|
||||||
pwFile := fmt.Sprintf("%s%s", ConfigDir, "pw")
|
pwFile := fmt.Sprintf("%s%s", ConfigDir, "pw")
|
||||||
pass, err := util.ReadFile(pwFile)
|
pass, err := util.ReadPWFile(pwFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
havePassword = false
|
havePassword = false
|
||||||
// See if we have a password...
|
// See if we have a password...
|
||||||
@ -46,6 +48,7 @@ func runRofi(cmd *cobra.Command, args []string) error {
|
|||||||
} else {
|
} else {
|
||||||
havePassword = true
|
havePassword = true
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err = models.NewKeePassDB(viper.GetString("database"), pass)
|
db, err = models.NewKeePassDB(viper.GetString("database"), pass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -6,26 +6,33 @@ package util
|
|||||||
import (
|
import (
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"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) {
|
func Encrypt(key, input string) (string, error) {
|
||||||
key, _ := hex.DecodeString(key)
|
btKey, _ := hex.DecodeString(key)
|
||||||
plaintext := []byte(input)
|
plaintext := []byte(input)
|
||||||
|
|
||||||
// Create a new Cipher Block from the key
|
// Create a new Cipher Block from the key
|
||||||
block, err := aes.NewCipher(key)
|
block, err := aes.NewCipher(btKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
||||||
iv := ciphertext[:aes.BlockSize]
|
iv := ciphertext[:aes.BlockSize]
|
||||||
if _, err := io.ReadFull(rand.reader, iv); err != nil {
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,16 +44,16 @@ func Encrypt(key, input string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Decrypt(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)
|
ciphertext, _ := base64.URLEncoding.DecodeString(input)
|
||||||
|
|
||||||
block, err := aes.NewCipher(key)
|
block, err := aes.NewCipher(btKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ciphertext) < aes.BlockSize {
|
if len(ciphertext) < aes.BlockSize {
|
||||||
return errors.New("ciphertext is too short")
|
return "", errors.New("ciphertext is too short")
|
||||||
}
|
}
|
||||||
iv := ciphertext[:aes.BlockSize]
|
iv := ciphertext[:aes.BlockSize]
|
||||||
ciphertext = ciphertext[aes.BlockSize:]
|
ciphertext = ciphertext[aes.BlockSize:]
|
||||||
|
@ -3,7 +3,9 @@ Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
|||||||
*/
|
*/
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import "io/ioutil"
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
func ReadFile(path string) (string, error) {
|
func ReadFile(path string) (string, error) {
|
||||||
var bytesRead []byte
|
var bytesRead []byte
|
||||||
@ -14,6 +16,22 @@ func ReadFile(path string) (string, error) {
|
|||||||
return string(bytesRead), nil
|
return string(bytesRead), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func WritePWFile(path, contents string) error {
|
func ReadPWFile(path string) (string, error) {
|
||||||
return ioutil.WriteFile(path, []byte(contents), 0600)
|
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