added encryption based pasword storing

This commit is contained in:
Jannover5000r 2025-08-05 00:10:38 +02:00
parent b8d9cb5c82
commit ac69fb6780
2 changed files with 84 additions and 2 deletions

View file

@ -1,13 +1,21 @@
package bot package bot
import ( import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io"
"os" "os"
"os/signal" "os/signal"
"sync" "sync"
"syscall" "syscall"
"github.com/joho/godotenv"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -30,6 +38,66 @@ var (
accountsFile = "accounts.json" accountsFile = "accounts.json"
) )
var encryptionKey []byte
func init() {
godotenv.Load("/home/Jannik/Documents/Code/Go/Untis/Untis-Logger/.env")
keyStr := os.Getenv("ENC_KEY")
if keyStr == "" {
panic("ENC_KEY environment variable not set")
}
key, err := base64.StdEncoding.DecodeString(keyStr)
if err != nil {
panic("ENC_KEY is not valid base64")
}
if len(key) != 32 {
panic("ENC_KEY must be exactly 32 bytes after base64 decoding")
}
encryptionKey = key
}
func encrypt(text string) (string, error) {
block, err := aes.NewCipher(encryptionKey)
if err != nil {
return "", err
}
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonce := make([]byte, aesGCM.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}
ciphertext := aesGCM.Seal(nonce, nonce, []byte(text), nil)
return base64.StdEncoding.EncodeToString(ciphertext), nil
}
func decrypt(encrypted string) (string, error) {
data, err := base64.StdEncoding.DecodeString(encrypted)
if err != nil {
return "", err
}
block, err := aes.NewCipher(encryptionKey)
if err != nil {
return "", err
}
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonceSize := aesGCM.NonceSize()
if len(data) < nonceSize {
return "", errors.New("ciphertext too short")
}
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
}
return string(plaintext), nil
}
// Save account info to JSON file (appends or updates) // Save account info to JSON file (appends or updates)
func saveAccount(userID, username, password string) error { func saveAccount(userID, username, password string) error {
var accounts []Account var accounts []Account
@ -39,11 +107,24 @@ func saveAccount(userID, username, password string) error {
_ = json.Unmarshal(data, &accounts) _ = json.Unmarshal(data, &accounts)
} }
// Append new account // Remove any account with the same userID
newAccounts := accounts[:0]
for _, acc := range accounts {
if acc.UserID != userID {
newAccounts = append(newAccounts, acc)
}
}
accounts = newAccounts
// Encrypt the password before saving
encPwd, err := encrypt(password)
if err != nil {
return err
}
accounts = append(accounts, Account{ accounts = append(accounts, Account{
UserID: userID, UserID: userID,
Username: username, Username: username,
Password: password, Password: encPwd,
}) })
// Save back to file // Save back to file

View file

@ -4,5 +4,6 @@
- UNTIS_USER - UNTIS_USER
- DISCORD_WEBHOOK_URL - DISCORD_WEBHOOK_URL
- DISCORD_BOT_TOKEN - DISCORD_BOT_TOKEN
- ENC_KEY (generated via head -c 32 /dev/urandom | base64)
# This is a project for me, issues will be resolved as i find the motivation to do that, improvements may follow in the future # This is a project for me, issues will be resolved as i find the motivation to do that, improvements may follow in the future